In this article we will discuss how to recursively add all files, folders, and sub folders of the project to the staging area of git in a single command and then finally commit to the repository. Also, we will cover scenarios where we need to recursively add all files & sub folders of a specific folder only to the staging area and then commit to repository.


Many times, we encounter a situation where we need to add multiple files, folders, sub folders and files under those folders to git. Basically, complete chunk of nested folder structure needs to be added to git. Git provides a single command for that i.e.

Git Command to recursively add all files / folders of the project to stagging area

git add -A

or
git add --all

It adds all the new, modified & deleted files throughout the project to the staging area irrespective of location you are running this command from.

Git Command to recursively add all files / sub-folders only in current directory to stagging area

If you want to add files & sub folders from the current folder only, then use following command,

git add .

Let’s understand with some examples,

Using “git add -A” to recursively add all new, modified & deleted files / folders to git

Tracked files are files about which git knows i.e. which are either in staging area or already committed to the git repository. Whereas untracked files are those which are new in project and git does not know about them. Let’s see how to add changes in both types of files to staging area in a single command.

Suppose we are in our master branch of our project, now we will check the git status of project,

$ git status

Output:
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.md
        deleted:    notes.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        build.xml
        include/
        src/
        test/

no changes added to commit (use "git add" and/or "git commit -a")

We have following changes waiting to be added in staging area of git,

  1. Many untracked files i.e. new files, folders, sub folders and files under those folders.
  2. One modified tracked filed i.e README.md
  3. One tracked file is deleted i.e. notes.txt

Now we can add all these changes to the staging area using a single command i.e.

$ git add -A

It recursively added all the changes i.e. new/modified/deleted files in folders and in sub folders to the staging area of git. We can confirm this by checking the git status of the project i.e.
$ git status

Output:
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md
        new file:   build.xml
        new file:   include/utility.h
        new file:   src/mainfile.cc
        new file:   src/utility.cc
        renamed:    notes.txt -> test/first.test
        new file:   test/lib/testutil.lib
        new file:   test/second.test

-A is a shorthand form of “—all”, so we can use this too for the same operation i.e.
$ git add –-all

It will have same effect as -A parameter i.e. it will recursively add all files and folders to the staging area of git.

Now let’s commit all the files we just added to the staging area i.e.

$ git commit -m "Adding all files & folders under the project"

It will commit all the files in the  stagging area.

Important point about “git add -A”

In above example we executed the command from root location of project, but even if we execute this command from any location in our project, it will add files in the project to the staging area of the git irrespective of location from where we are executing this command. So, if you want to recursively add all files of a specific folder or from the current location of project only, instead of all files in project, then there is an another command for that. Let’s discuss that,

Using “git add .” to recursively add all files/sub-folders of a specific folder only to git

Suppose we are in our master branch of our project, now we will check the git status of project,

$ git status

Output:
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.md
        deleted:    notes.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        build.xml
        include/
        src/
        test/

no changes added to commit (use "git add" and/or "git commit -a")

We have following changes waiting to be added in staging area of git,

  1. Many untracked files i.e. new files, folders, sub folders and files under those folders.
  2. One modified tracked filed i.e README.md
  3. One tracked file is deleted i.e. notes.txt

But we don’t want all changes to be added to the staging area of git, we only want files & folders under the test folder to the staging area only. Let’s check the contents of test folder only i.e.

$ ll test/

Output:
total 0
-rw-r--r-- 1 varun 197609 0 Jun  2 20:27 first.test
drwxr-xr-x 1 varun 197609 0 Jun  2 20:27 lib/
-rw-r--r-- 1 varun 197609 0 Jun  2 20:27 second.test

Now to do that, we will go inside the test folder,
$ cd test/
$ pwd

Output:
/e/Projects/testapp/test

Now let’s execute to a command to add files under this folder only to the staging area i.e.
$ git add .

It added all the files in test folder only to the staging area. We can confirm this by using git status command i.e.
$ git status

Output:
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   first.test
        new file:   lib/testutil.lib
        new file:   second.test

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   ../README.md
        deleted:    ../notes.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        ../build.xml
        ../include/
        ../src/

It shows all the changes which were outside the test folder are yet to be added to staging area. It recursively added all the files and folders from current location of the project only.

If we would have executed the “git add -A” from the test folder of the project, then it would have added all the files in complete project to the staging area, irrespective to the fact that we were in test folder only. So, where as “git add .” added the all files / changes in current folder only.

Now let’s commit all the files we just added to the staging area i.e.

$ git commit -m "Adding files & folders under the test folder of project"

Output:
[master 5eadb58] Adding files & folders under the test folder of project
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test/first.test
 create mode 100644 test/lib/testutil.lib
 create mode 100644 test/second.test

So, to recursively add all files or folders and also sub folders to the staging area of git, we can either call “git add -A” or “git add –all”, it will add all files in the project workspace to the staging area, irrespective of location from where this command is executing. Whereas in a project if you want to recursively add all files and folders in current directory only  then use “git add .” .

Subscribe to our Newsletter

Join  a list of 2000+ Python & C++ Developers and get more Tips & Tutorials like this.