Home Software Engineering How To Git Add All Files

How To Git Add All Files

by schkn

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

git add all files

In order to add all those files to our staging area, we will use the “git add” command followed by the “-A” option.

files added to working directory

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

As an example, let’s say that you have created two Javascript files and one text file.

add files by extension

In order to add all Javascript files, we are going to use the wildcard syntax followed by “*.js”.

$ git add *.js
wildcard git add

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

git add dot syntax

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 .
add specific folder git

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>/.
git add absolute 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.

git add chart

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.

git add all deleted files

In order to add the deleted and modified files only, we are going to execute “git add” with the “-u” option.

$ git add -u .
git status command on directory

Awesome, you successfully added all your deleted and modified files to your current Git repository!

Conclusion

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!

software engineering

You may also like

1 comment

Zain Ul Basit April 2, 2023 - 12:14 am

Informative….

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.