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:
$ git init |
Add by filename
Use git add [<pathspec>…]
to add file by their name.
If we want to add our two files created above:
$ git add file1.txt file2.txt |
It is also possible to use wildcards
$ 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.
$ 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
$ echo change > file-modify.txt # modifiy a file |
At this point, any of the command above will stage all these files:
$ git add -A # add all files to the next commit |
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:
$ git add -u |
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:
$ git add --ignore-removal |
Example:
$ git add --ignore-removal |
Try it yourself
Here is the full script if you want to see it with your own eyes :)
git init |
Hope this gives you a clearer view of what is going on !