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:
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:
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:
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:
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.