Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `git add .` and `git add -u`?

Tags:

git

git-add

I was assuming that both work in the same way. Both add every file onto index. But I seem wrong.

  • What's the difference between git add . and git add -u?
like image 336
TK. Avatar asked Feb 03 '10 07:02

TK.


People also ask

Is git add the same as git add?

Detail: git add -A is equivalent to git add .; git add -u . The important point about git add . is that it looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored, it does not stage any 'rm' actions.

What is git add?

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .

What is the difference between git add and git commit?

git add : takes a modified file in your working directory and places the modified version in a staging area. git commit takes everything from the staging area and makes a permanent snapshot of the current state of your repository that is associated with a unique identifier.


1 Answers

It is one of the git gotchas mentioned here (pre Git 2.0).

git add . only adds what is there, not what has been deleted (if tracked).

git add . git commit git status //hey! why didn't it commit my deletes?, Oh yeah, silly me git add -u . git commit --amend 

git add -A would take care of both steps...


With Git 2.0, git add -A is default.

git add <path> is the same as "git add -A <path>" now, so that "git add dir/" will notice paths you removed from the directory and record the removal.
In older versions of Git, "git add <path>" used to ignore removals.

You can say "git add --ignore-removal <path>" to add only added or modified paths in <path>, if you really want to.


Warning (git1.8.3 April 2013, for upcoming git2.0).
I have modified my answer to say git add -u ., instead of git add -u.:

git add -u will operate on the entire tree in Git 2.0 for consistency with "git commit -a" and other commands.
Because there will be no mechanism to make "git add -u" behave as "git add -u .", it is important for those who are used to "git add -u" (without pathspec) updating the index only for paths in the current subdirectory to start training their fingers to explicitly say "git add -u ." when they mean it before Git 2.0 comes.

As I mentioned in "e"

like image 172
VonC Avatar answered Oct 17 '22 10:10

VonC