77

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?
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
TK.
  • 27,073
  • 20
  • 64
  • 72

3 Answers3

79

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"

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 6
    Thanks for your answer and an example. The "hey!" line really helps me. – TK. Feb 03 '10 at 14:15
  • @TK: yes, Benjol (http://stackoverflow.com/users/11410/benjol)'s example is a good one. – VonC Feb 03 '10 at 14:18
  • It seems we don't need to put the `.`(pathspec) with Git 2.x. It works find when I tested. – Sanghyun Lee Jul 24 '15 at 03:26
  • Also, from Git 2.0, `git add .` adds all changes to index as `-A` option is default. Check this answer http://stackoverflow.com/a/26343584/524588 – Sanghyun Lee Jul 24 '15 at 03:28
  • @Sangdol Thank you for this reminder. I have updated the answer accordingly. – VonC Jul 24 '15 at 05:57
  • Very helpful thanks. Unfortunately this is bad design. The property -u should have become the update command all by its own. Laziness? Mountain of technical debt? – Salvador Valencia Mar 08 '18 at 20:36
25

Like the manual says: git add . will add all files in the current directory, whereas git add -u . will only add those already being tracked.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
14

git add documentaiton

git add . 

add all files from the current directory

git add -u 

only update files currently being tracked.

Derek Swingley
  • 8,734
  • 5
  • 32
  • 33