Git add command explained with examples.


After a developer completes a software product’s functionality, the next step is to publish all changes into the repository. All modified and newly created files should be staged for the next commit.

The git “add” command creates a staging area that contains updated, added, and deleted files.

Adding a new file to Git, an step by step example.

Step-1: Create a new file named addExampleFile.c in the working tree using vim or other ways.

ls -lrt
total 16
-rw-r--r--. 1 root root 124 Jul 18 05:49 README.md
-rw-r--r--. 1 root root  10 Jul 18 05:49 newFile.txt
-rw-r--r--. 1 root root  33 Jul 18 05:49 hello.c
-rw-r--r--. 1 root root  43 Jul 19 03:30 addExampleFile.c

Step-2: The file has been created and untracked by git.

# git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        addExampleFile.c

nothing added to commit but untracked files present (use "git add" to track)

Step-3: With the help of the git add command, let’s make the local repository aware of this new file.

# git add addExampleFile.c
[root@165 - CenOs7  projectsource]# git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   addExampleFile.c

Step-4: Using git commit, add the code changes to the repository.

# git commit -m "Commit for file addExampleFile.c"
[master 45bb6d6] Commit for file addExampleFile.c
 1 file changed, 1 insertion(+)
 create mode 100644 addExampleFile.c
[root@165 - CenOs7  projectsource]# git log
commit 45bb6d6f57d51199bc74c853b1957804d3805b8a (HEAD -> master)
Author: gituser <gituser@gmail.com>
Date:   Wed Jul 19 06:08:13 2023 -0400

    Commit for file addExampleFile.c

commit 0ad66837f7c67253e6b8d74d10c5230ef1e38b19 (origin/master, origin/HEAD)
Author: root <root@localhost.localdomain>

Step -5: With the git push command, propagate the changes to the remote repository.

All options in git add commands are explained.

This section will cover all options available in the git add command, along with a practical example for each.

Add individual files or directories (file-path/directory).

If you specify the full path to the file, the file will get added, and if you specify the directory path, all files in the directory will be added. You can specify multiple files or directories separated by space as command-line options.

Add everything to the staging area with the –all option for the next commit.

Using this option will add all changes inside the project for the next commit. Any deleted or modified files, as well as new files, will be added. However, be aware that this may add unwanted files, such as swap files, binaries, tags on Linux, etc.

git add --all

Add changes only to files that have been tracked (-u).

It adds all the changes to the staging area. A change includes either modified files or deleted files. The untracked files remain untouched.