Oscar the owl and Vershd logo

Add Files to Git Commit

28th August 2020

This is part of a Git Tips series. For the complete set for beginner and intermediate Git users, see our white paper.

The Problem

If you’ve got multiple files to add (stage), and you’re working to a deadline, you’ll want to know how to add files to Git commit as quickly and simply as possible, and quite possibly how to add all files to Git commit.

You can add files one by one to prevent any costly mistakes. But if you know what you’re doing understanding how to add all files at once can streamline your workflow and speed up deployment.

As is the often the case with Git there are several different ways to add all files ready for commit, with slight differences in each command. We’ll explain them all.

Information: we assume you’re working with Git version 2.x with these commands.

Add All Modified Files

The quickest way to add all the modified files you’ve been working on to your local repo is with the update command:
git add -u
The -u is shorthand for --update. But this command will only stage modified and deleted files.

Add All New, Modified & Deleted Files

There are two command options to add all new, modified and deleted files. You can use:
git add -A
Or
git add .
The difference is where the staged files are sourced from.

git add . will only add files located in the current path you’re working in. So, if you’re in a root directory, it will only add files located there, and not any sub directories.

So the best option to add all files is git add –A. The -A is shorthand for --all, it will find all files from every directory in your project and stage them all.

Warning: this command will also update deleted files and remove them from staging.

Add All New & Modified but Not Deleted Files

If you want to keep files in your repository even if they no longer exist in your local directory, you’ll need to use a different command to add all new and modified files:
git add --ignore-removal
It does what it says on the tin and ignores any files that have been removed/deleted.

Commit the Staged Files

Using the above commands, you’ll have added all files in your directory to staging, ready for commit. Now it’s simply a case of using the standard git commit command:
git commit -m "your message"
And all your files have now been committed. If you want to add and commit all tracked, modified files in a single command you could use:
git commit -am "your message"
However, this command will only add and commit all modified files. It does not add any newly created files. So the best option if you want to add all files to Git Commit, is to use the following two commands:
git add -A
git commit -m "your message"
Or you can use them as a single command line in the following way, if you want to minimise entries:
git add -A && git commit -m "your Message"

Commit the Staged Files Using an Alias

The only minor problem you may find with the above command line is that it’s long winded. Typing it out repeatedly could slow your workflow down. So, the alternative option is to create your own alias. All you need to do is set up your own shorthand for multi-command lines:

• Open up your home directory
• Located your git.config file
• Add the following command xx = !git add -A && git commit -m
• Replace xx with your desired alias, and you’re done.

Now all you have to do when you want to add and commit all files (including new ones) in a single command is type:
git xx "your message"
Simple and effective. In general, Git commits should be regular and modular, preferably affecting only a few files, otherwise you may run into accidental deletions and unwanted errors.

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram