3113

The command git add [--all|-A] appears to be identical to git add .. Is this correct? If not, how do they differ?

CharlesB
  • 75,315
  • 26
  • 174
  • 199
cmcginty
  • 101,562
  • 37
  • 148
  • 155

12 Answers12

4503

This answer only applies to Git version 1.x. For Git version 2.x, see other answers.


Summary:

  • git add -A stages all changes

  • git add . stages new files and modifications, without deletions (on the current directory and its subdirectories).

  • git add -u stages modifications and deletions, without new files


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.

git add -u looks at all the already tracked files and stages the changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.

git add -A is a handy shortcut for doing both of those.

You can test the differences out with something like this (note that for Git version 2.x your output for git add . git status will be different):

git init
echo Change me > change-me
echo Delete me > delete-me
git add change-me delete-me
git commit -m initial

echo OK >> change-me
rm delete-me
echo Add me > add-me

git status
# Changed but not updated:
#   modified:   change-me
#   deleted:    delete-me
# Untracked files:
#   add-me

git add .
git status

# Changes to be committed:
#   new file:   add-me
#   modified:   change-me
# Changed but not updated:
#   deleted:    delete-me

git reset

git add -u
git status

# Changes to be committed:
#   modified:   change-me
#   deleted:    delete-me
# Untracked files:
#   add-me

git reset

git add -A
git status

# Changes to be committed:
#   new file:   add-me
#   modified:   change-me
#   deleted:    delete-me
Honey
  • 24,125
  • 14
  • 123
  • 212
CB Bailey
  • 648,528
  • 94
  • 608
  • 638
  • 43
    How about the difference between `git add *`? – Jared Mar 24 '12 at 00:52
  • 1
    I tried this, and it seems to have added files that were in my .gitignore. Is there a way to do the same thing, but honor .gitignore? – Victor Engel May 27 '13 at 16:45
  • 3
    too bad `git add -A -p` doesn't work as one would expect (ask interactively about untracked files) – Erik Kaplun Feb 15 '14 at 22:52
  • 1
    I received a terminal message that Git was looking for `--all :/`. Does this answer need to be updated for any recent changes in the API to include ` :/` ? – lawlist Apr 25 '14 at 05:29
  • 2
    Please update the answer. It should be: `git add -A :/` or `git add -A .` – Gabriel Llamas May 04 '14 at 08:19
  • 1
    @GabrielLlamas: Please see here: http://stackoverflow.com/help/privileges/edit ; you should have sufficient privileges to make edits yourself. – CB Bailey May 04 '14 at 12:03
  • 1
    Do Caps matter? (i.e. 'git add -A' vs. 'git add -a') – Mazzone May 15 '14 at 15:48
  • 1
    Hi @Mazzone, there's no such command as `git add -a`, caps matter – ajdeguzman May 21 '14 at 09:27
  • 5
    For information, in newer versions of git `git add -u` has become `git add -u :/` with the latter parameter being a path, allowing you to -u certain directories, `:/` handles the whole tree. – Brizee Jun 05 '14 at 09:49
  • 17
    @CharlesBailey, Git really *love* making things complicated for no good reason. Is there a **real** use case whereby someone would specifically need `git add -u` or `git add .` and by doing that it makes his life easier even after accounting for the extra mental tax added to ensure that there're no sync problems? I wonder why Git doesn't furthur split `add -u` into two separate commands `add -u1` and `add-u2` whereby one works for files starting with numerals and the other for files starting with non-numerals – Pacerier Oct 20 '15 at 10:15
  • @CharlesBailey I make a .gitignore file near .git file, with a directory path inside it. but with your command, it doesn't exclude that folder. – Dr.jacky Jul 11 '16 at 10:10
  • @CharlesBailey But why I get error "blahblah did not match any file(s) known to git" on commit?! Even get this error with "git ls-files blahblah --error-unmatched" command. I have one commit and see whole project files with "git ls-tree --name-only -r sha1" command. – Dr.jacky Oct 08 '16 at 07:06
  • In the case of 'git add .' what does not staging a deleted file even do? On commit the file is deleted in your working copy but not in the repo, until you pull and then what happens, you get the file back again? Anyone remotely familiar with normal command line usage would expect 'git add .' to process the entire current location. Way to go git for making even the simplest task as obtuse as possible. – Neutrino Sep 16 '17 at 12:45
  • @yuan-ji be careful when editing to avoid removing critical information. – Emile Bergeron Jan 12 '18 at 03:54
  • 2
    What is git add * ? There are none of description. – creator Mar 16 '18 at 07:15
  • 1
    This is wrong, `git add .` *does* add deleted files in your working copy. They get staged as deleted. – void.pointer Aug 18 '18 at 13:44
  • ```sh git add -A stages all changes git add . stages new files and modifications, without deletions git add -u stages modifications and deletions, without new files ``` – xgqfrms Aug 15 '19 at 09:03
  • `git add *` doesn't add _hidden_ files, while `git add .` & `git add -A` do add hidden files – Honey Feb 20 '21 at 20:20
  • 1
    @Pacerier Well, I modified a whole bunch of files and didn't want to type them all out, and yet I also had a whole bunch of files I wanted to keep around but didn't want to be in the repo. I could have added those file names to gitignore, but that also is work, and in my case, not what I wanted to do. So yeah, I'm rather glad `add -u` was there. – Jacob Lee Mar 03 '21 at 21:24
1092

Git Version 1.x

Command New Files Modified Files Deleted Files Description
git add -A ✔️ ✔️ ✔️ Stage all (new, modified, deleted) files
git add . ✔️ ✔️ Stage new and modified files only in current folder
git add -u ✔️ ✔️ Stage modified and deleted files only

Git Version 2.x

Command New Files Modified Files Deleted Files Description
git add -A ✔️ ✔️ ✔️ Stage all (new, modified, deleted) files
git add . ✔️ ✔️ ✔️ Stage all (new, modified, deleted) files in current folder
git add --ignore-removal . ✔️ ✔️ Stage new and modified files only
git add -u ✔️ ✔️ Stage modified and deleted files only

Long-form flags:

  • git add -A is equivalent to git add --all
  • git add -u is equivalent to git add --update

Further reading:

TheGeorgeous
  • 3,374
  • 1
  • 18
  • 30
Developer
  • 21,956
  • 19
  • 72
  • 119
  • 2
    Thanks for the table. Is there a way to add only the files that were modified. No new files or deleted files – Gokul N K Jan 13 '15 at 12:43
  • 3
    @Gokul: According to [this post](http://stackoverflow.com/a/14368373/134536), you can use `git diff-files -z --diff-filter=M --name-only | xargs -0 git add` to add only the modified files, but not the new files or the deletions. – Ville May 08 '15 at 05:41
  • 108
    This is not entirely true, as `git add .` only adds new files that are on the current path. I.e. if you have a new directory `../foo`, `git add -A` will stage it, `git add .` will not. – Milo Wielondek Jul 06 '15 at 12:44
  • 3
    So, `git add .` is equivalent to `git add -A .`, which is equivalent to `git add "*"` – flow2k Mar 25 '18 at 23:21
  • I m still confuse about git add "*", can you please elaborate it little more? – H S Umer farooq Nov 21 '18 at 15:12
  • Is the answer for Git Version 1.x still relevant in 2021? – np8 Apr 19 '21 at 14:40
188

With Git 2.0, git add -A is default: git add . equals git add -A ..

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>" ignored removals.

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

git add -A is like git add :/ (add everything from top git repo folder).
Note that git 2.7 (Nov. 2015) will allow you to add a folder named ":"!
See commit 29abb33 (25 Oct 2015) by Junio C Hamano (gitster).


Note that starting git 2.0 (Q1 or Q2 2014), when talking about git add . (current path within the working tree), you must use '.' in the other git add commands as well.

That means:

"git add -A ." is equivalent to "git add .; git add -u ."

(Note the extra '.' for git add -A and git add -u)

Because git add -A or git add -u would operate (starting git 2.0 only) on the entire working tree, and not just on the current path.

Those commands 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 if "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.

A warning is issued when these commands are run without a pathspec and when you have local changes outside the current directory, because the behaviour in Git 2.0 will be different from today's version in such a situation.

Robin A. Meade
  • 1,003
  • 10
  • 12
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 3
    @NickVolynkin That's great! Glad to see the international community of SO working as intended. For reference: http://ru.stackoverflow.com/a/431840 – VonC Jun 26 '15 at 20:53
  • @VonC, Nice, the Git folks actually had the cheek to say their update will "make things more consistent". What they had done is created more confusion and inconsistencies. There's 26 alphabets and they *had* to reuse a flag that's already been used. – Pacerier Oct 20 '15 at 10:06
141

From Charles' instructions, after testing my proposed understanding would be as follows:

# For the next commit
$ git add .   # Add only files created/modified to the index and not those deleted
$ git add -u  # Add only files deleted/modified to the index and not those created
$ git add -A  # Do both operations at once, add to all files to the index

This blog post might also be helpful to understand in what situation those commands may be applied: Removing Deleted Files from your Git Working Directory.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Richard
  • 1,411
  • 1
  • 9
  • 2
  • 8
    this is no longer true in 2.0. add . equals to add -A for the same path, the only difference is if there are new files in other paths of the tree – Claudiu Creanga Sep 27 '15 at 23:38
44

Things changed with Git 2.0 (2014-05-28):

  • -A is now the default
  • The old behavior is now available with --ignore-removal.
  • git add -u and git add -A in a subdirectory without paths on the command line operate on the entire tree.

So for Git 2 the answer is:

  • git add . and git add -A . add new/modified/deleted files in the current directory
  • git add --ignore-removal . adds new/modified files in the current directory
  • git add -u . adds modified/deleted files in the current directory
  • Without the dot, add all files in the project regardless of the current directory.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
0xF
  • 2,059
  • 1
  • 18
  • 22
  • 4
    I don't think this is correct. Using git v2.10.windows.2 'git add' returns 'Nothing specified, nothing added'. 'git add -A' adds all changed files. Which suggests '-A' is not the default. – Neutrino Oct 05 '17 at 12:47
  • Last point "Without the dot, add all files in the project regardless of the current directory." doesn't work. When I say {code}git add{code} (without .) then for a mesage with hint specifying if I want to say {code}git add . {code} – user85 Aug 11 '20 at 06:55
37

A more distilled quick answer:

Does both below (same as git add --all)

git add -A

Stages new + modified files

git add .

Stages modified + deleted files

git add -u
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
K. Kilian Lindberg
  • 2,668
  • 20
  • 27
  • 5
    Hello, what if you just wanted to stage only modified files? How would you do that? – TheGrapeBeyond May 26 '14 at 16:06
  • 2
    Hello, well good question. There isn't an easy flag for that as far as i know.. git diff-files -z --diff-filter=M --name-only | xargs -0 git add from -> http://stackoverflow.com/questions/14368093/simpler-way-to-stage-only-modified-files-in-git-not-deleted – K. Kilian Lindberg May 28 '14 at 13:19
  • 3
    Actually it's `git add :/` + `git add -u :/` – Nick Volynkin Jul 24 '15 at 10:55
36

In Git 2.x:

  • If you are located directly at the working directory, then git add -A and git add . work without the difference.

  • If you are in any subdirectory of the working directory, git add -A will add all files from the entire working directory, and git add . will add files from your current directory.

And that's all.

simhumileco
  • 21,911
  • 14
  • 106
  • 90
12

I hope this may add some more clarity.

!The syntax is
git add <limiters> <pathspec>
! Aka
git add (nil/-u/-A) (nil/./pathspec)

Limiters may be -u or -A or nil.

Pathspec may be a filepath or dot, '.' to indicate the current directory.

Important background knowledge about how Git 'adds':

  • Invisible files, those prefixed with a dot, (dotfiles) are never automatically recognized by Git. They are never even listed as 'untracked'.
  • Empty folders are never added by Git. They are never even listed as 'untracked'. (A workaround is to add a blank file, possibly invisible, to the tracked files.)
  • Git status will not display subfolder information, that is, untracked files, unless at least one file in that subfolder is tracked. Before such time, Git considers the entire folder out of scope, a la 'empty'. It is empty of tracked items.
  • Specifying a filespec = '.' (dot), or the current directory, is not recursive unless -A is also specified. Dot refers strictly to the current directory - it omits paths found above and below.

Now, given that knowledge, we can apply the answers above.

The limiters are as follows.

  • -u = --update = subset to tracked files => Add = No; Change = Yes; Delete = Yes. => if the item is tracked.
  • -A = --all (no such -a, which gives syntax error) = superset of all untracked/tracked files , unless in Git before 2.0, wherein if the dot filespec is given, then only that particular folder is considered. => if the item is recognized, git add -A will find it and add it.

The pathspec is as follows.

  • In Git before 2.0, for the two limiters (update and all), the new default is to operate on the entire working tree, instead of the current path (Git 1.9 or earlier),
  • However, in v2.0, the operation can be limited to the current path: just add the explicit dot suffix (which is also valid in Git 1.9 or earlier);

git add -A .

git add -u .

In conclusion, my policy is:

  1. Ensure any hunks/files to be added are accounted for in git status.
  2. If any items are missing, due to invisible files/folders, add them separately.
  3. Have a good .gitignore file so that normally only files of interest are untracked and/or unrecognized.
  4. From the top level of the repository, "git add -A" to add all items. This works in all versions of Git.
  5. Remove any desired items from the index if desired.
  6. If there is a big bug, do 'git reset' to clear the index entirely.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
AnneTheAgile
  • 8,897
  • 6
  • 42
  • 45
12

git add . equals git add -A . adds files to index only from current and children folders.

git add -A adds files to index from all folders in working tree.

P.S.: information relates to Git 2.0 (2014-05-28).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alex78191
  • 1,276
  • 1
  • 12
  • 22
11

Both git add . and git add -A will stage all new, modified and deleted files in the newer versions of Git.

The difference is that git add -A stages files in "higher, current and subdirectories" that belong to your working Git repository. But doing a git add . only stages files in the current directory and subdirectories following it (not the files lying outside, i.e., higher directories).

Here's an example:

/my-repo
  .git/
  subfolder/
    nested-file.txt
  rootfile.txt

If your current working directory is /my-repo, and you do rm rootfile.txt, then cd subfolder, followed by git add ., then it will not stage the deleted file. But doing git add -A will certainly stage this change no matter where you perform the command from.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Sabbir Ahmed
  • 899
  • 1
  • 7
  • 13
3

The -A option adds, modifies, and removes index entries to match the working tree.

In Git 2 the -A option is now the default.

When a . is added that limits the scope of the update to the directory you are currently in, as per the Git documentation

If no <pathspec> is given when -A option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

One thing that I would add is that if the --interactive or -p mode is used then git add will behave as if the update (-u) flag was used and not add new files.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Yehuda Schwartz
  • 2,459
  • 1
  • 21
  • 32
0

It's useful to have descriptions of what each flag does. By using a CLI like bit you'll have access to flag descriptions as you're typing.

Chris W
  • 735
  • 7
  • 17