Git add recursively – Git : How to recursively add all files or folders to a repository?

How to recursively add all files or folders to a repository ?

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

Git add recursively: It adds all the files(new, modified or even deleted) that were there throughout the project irrespective of the location from where the command is being executed.

git add -A                   

OR

git add –all

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

Git add all files in folder: The above command is okay for all files but if we want to add files from the active directory only we can use

git add .

Important point about “git add -A”

Git add all files: The command git add -A can be executed from any directory. However, it will only move the files  to the staging area from the project folder only. To add files from a specific folder we have to use the other command git add .

Using “git add -A”

Git add all files: We will first check the status of our git project in the master branch

$ 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:   imp.md
        modified:  notes.txt
        deleted:    ignore.md
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. Two modified tracked files i.e README.md and notes.txt
  3. One tracked file is deleted i.e. ignore.md

Now to add these changes to the staging area,

$ git add -A

We can recheck the status to check if all our files have moved to the staged area

$ git status

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

Finally we can commit the files in the staging area

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

Using “git add .”

How to git add all: Let’s take the same situation for this example too

$ 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:   imp.md
        modified:  notes.txt
        deleted:    ignore.md
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. Two modified tracked files i.e README.md and notes.txt
  3. One tracked file is deleted i.e. ignore.md

However we dont want to add all the files, we only want files from a particular folder to be added say test folder. Let’s check what files are in test folder

$ ll test/

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

Now to move the files, we will go inside the test folder,

$ cd test/


$ pwd

From here we can add the files from this folder to the staging area

$ git add .


$ 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/

This shows that all the files from test folder were added however he other folder files are not added. Finally commit

$ 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