When working on software projects, it is quite common for developers to add new files to their repositories.
In some cases, you may want to change some of your existing files. In other cases, you may want to add untracked files to your repository.
In both cases, you will need to use the same Git command : Git Add.
In this tutorial, you will learn how you can easily add all your files to your Git repository.
Determine your Git version
Depending on the current Git version that you installed on your computer, the “git add” command can differ.
To determine your current Git version, use “git” followed by the “–version” option.
$ git --version
Add All Files using Git Add
The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”.
$ git add -A $ git add . (at the root of your project folder)
In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.
Alternatively, you can use the “git add” followed by a “.” which stands for “current directory”. However, using the “.” syntax implies that you are currently at the top of your project folder hierarchy.
As an example, let’s say that we created a branch named “feature“. On this branch, we have three files : one deleted, one with its content modified and another one which is untracked.
In order to add all those files to our staging area, we will use the “git add” command followed by the “-A” option.
As you can see, the files were correctly added to the staging area, awesome!
You can now commit them to your Git repository in order for the changes to be shared with your colleagues.
Adding all files by extension
In some cases, you may be interested in adding all files that have a specific extension : *.txt or *.js for example.
To add all files having a specific extension, you have to use the “git add” command followed by a wilcard and the extension to add.
$ git add *.txt $ git add *.js
$ git add *.js
Congratulations, you successfully added your files having a specific extension!
Using dot with git add
As explained before, the “.” symbol stands for “current directory“.
As a consequence, if you don’t use it at the top of your project hierarchy, you will only add files in the current working directory.
To illustrate this concept, let’s say that you have two new files : “root-file” in your project top folder and “new-file” in a folder named “folder”.
If you navigate to your new folder and execute the “git add” command with the dot syntax, you will notice that you only add files located in this directory.
$ cd folder $ git add .
As a consequence, you might miss some of your files in your commit.
To avoid this problem, you can use the dot syntax combined with the absolute path to your project top folder.
$ git add <path>/.
Adding all files on older Git versions
In the first chapter, we use the “–version” option in order to check our current Git version.
This step is quite important because the behaviour of the “git add” command can differ depending on the Git version used.
If you use the dot syntax with an old Git version (less than 2.x), the modified and deleted files won’t be automatically added to your commit.
Adding deleted and modified files only
In order to add all deleted and modified files only to your staging area, you have to use the “git add” command followed by the “-u” option.
$ git add -u
As an example, let’s say that we modified one of our files, deleted one and added one to our working directory.
Using a simple “git status” command, we can inspect the state of our Git working directory.
In order to add the deleted and modified files only, we are going to execute “git add” with the “-u” option.
$ git add -u .
Awesome, you successfully added all your deleted and modified files to your current Git repository!
In this tutorial, you learnt how you can easily add all your files to your Git repository using the “git add” command.
You also learnt that you can use specific wildcards or options in order to add only deleted or modified files.
If you are interested in Software Engineering, we have a complete section dedicated to it on the website, so make sure to check it out!