How to Git Add All Files
When we are done modifying our files, we will be adding them to the staging area. The staging area is an intermediate step between making changes in the working directory and committing them to the repository. All the files present in the staging area are included in our next commit.
We use the Git Add command to add files to the staging area. Let's learn how to add all the files to the staging area at once.
Staging Files
Types of Files
Before using the Git Add command, we need to learn about how Git sees our files.
- We have new or untracked files. These are the newly created files that have not been added to the staging area even once and are not part of any commit.
- Then we have the modified files. These are the files that were previously added to the staging area and have been modified after that.
- Next, we have deleted files. Whenever a file is removed, this removal is also added to the staging area to be a part of the next commit. This is done so that our repository reflects the removal of the file.
The following Git Status command shows all the above cases.
When working with Git, we will encounter the above-mentioned changes that were made to different files. Let's learn how to add these types of changes to the staging area.
Adding All the Files to the Staging Area
We may have made changes to several files and want to include all these changes in the next commit. To do this, we need to add all the files to the staging area. The -A or the --all options can be used to stage all the files at once.
$ git add -A
Git also provides us with the .(period) sign by which we can add all the files of our current directory to the staging area.
$ git add .
For example, consider we have two folders, f1 and f2, in our repository and we have made changes to files in both of these folders, and we are currently working in the f1 folder. Now if we run the $ git add .
command then only the files of the f1 folder will be added to the staging area.
But instead, if we are currently in the top directory of our repository and run this command then the changes of all the sub-directories will be staged.
Adding all the New and Modified Files
As discussed above, we can have new, modified, or deleted files present in our Git Repository. If we want to add all the new and modified files to the staging area but omit the deleted files, we can use the --ignore-removal option with the period sign.
$ git add --ignore-removal .
Consider the following scenario where we have a new file, a modified file, and a deleted file in our repository.
Now if we run the Git Add command with the --ignore-removal option, then all the files except the deleted file will be staged.
Adding all the Deleted and Modified Files
Git Add has the -u or the --update option which we can use to only stage the modified and deleted files and ignore the new untracked files.
$ git add -u .
Consider the following example with all three types of files.
When we run the Git Add command with the -u flag, then all files except the newly added file will be added to the staging area.
Using Glob Patterns to Stage Files
Glob patterns are used to match filenames that fit a certain description. For example, if we want all files with .py extension(python files) that start with the letter a, then the glob pattern will be a*.py. We can use such patterns to stage files. Simply enter the glob pattern after the Git Add command to stage those files.
$ git add Glob Pattern
Consider the following example in which only the python files were staged and the text documents were ignored.
Summary
Staging is an important part of any Git workflow. We will be often adding files to the staging area to make sure that they are part of the next commit. We can use the Git Add command to add files to the staging area. We can either specify individual file names or use more advanced options to make our simpler. In this tutorial, we learned how to stage different types of files by using different flags and options with the Git Add command. We also learned how to use Glob patterns to stage files that fit a certain description.