This article is a description of all the basic git add commands.

Understanding

Git uses a notion of changes staged for the next commit where you set which change you wish to include in the next git commit command. This includes new files, deleted files and modifications.

For those in a hurry, here is the conclusion of this article:

Command New Files Modified Files Deleted Files
git add -A V V V
git add . V V V
git add -u X V V
git add --ignore-removal V V X

Let’s start by initializing a new git repository:

1
2
3
4
$ git init
# Initialized empty Git repository in /.git/
$ echo test1 > file-delete.txt
$ echo test2 > file-modify.txt

Add by filename

Use git add [<pathspec>…​] to add file by their name.

If we want to add our two files created above:

1
$ git add file1.txt file2.txt

It is also possible to use wildcards

1
$ git add *.txt

After any of these two commands, both our files will be staged for the next commit. Let’s do our first commit before we continue.

1
$ git commit -m "Initial commit"

Stage all changes

If you wish to add deletions, updates and file creations, you can use any of these 3 commands:

  • git add -A
  • git add .
  • git add --all
1
2
3
4
5
6
7
8
$ echo change > file-modify.txt # modifiy a file
$ rm file-delete.txt # delete a file
$ echo new > file-new.txt # create a new file

$ git status
# deleted: file-delete.txt
# modified: file-modify.txt
# untracked: file-new.txt

At this point, any of the command above will stage all these files:

1
2
3
4
5
6
7
$ git add -A # add all files to the next commit

$ git status
# changes to be committed:
# deleted: file-delete.txt
# modified: file-modify.txt
# new file: file-new.txt

At this point, let’s do a git reset to revert the staged changes and demonstrate the other commands.

Stage modified and deleted files only

If you don’t want to add the newly created files, but only the modifications and deletions, use any of the following:

  • git add -u
  • git add -update

Example:

1
2
3
4
5
6
7
8
$ git add -u

$ git status
# changes to be committed:
# deleted: file-delete.txt
# modified: file-modify.txt
# untracked files:
# file-new.txt

Once again, a quick git reset will reset staged changes and allow us to look at the last option

Stage modified and new files only

If you want to stage everything except the files that have been deleted, use:

1
$ git add --ignore-removal

Example:

1
2
3
4
5
6
7
8
9
$ git add --ignore-removal

$ git status
# changes to be committed:
# modified: file-modify.txt
# new file: file-new.txt

# Changed but not updated:
# deleted: file-delete.txt

Try it yourself

Here is the full script if you want to see it with your own eyes :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git init
echo test1 > file-delete.txt
echo test2 > file-modify.txt
git add *.txt
git commit -m "Initial commit"
echo change > file-modify.txt
rm file-delete.txt
echo new > file-new.txt
git add -A
git status
git reset
git add -u
git status
git reset
git add --ignore-removal
git status

Hope this gives you a clearer view of what is going on !