Difference between "git add -A" and "git add ."

1,987,471

Solution 1

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

Solution 2

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:

Solution 3

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.

Solution 4

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.

Solution 5

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.
Share:
1,987,471
cmcginty
Author by

cmcginty

Interests include Python, Gradle, C, Java, Git, Linux, Embedded. Find me on github https://github.com/cmcginty.

Updated on July 08, 2022

Comments

  • cmcginty
    cmcginty almost 2 years

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

  • Jared
    Jared about 12 years
    How about the difference between git add *?
  • Victor Engel
    Victor Engel almost 11 years
    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?
  • Erik Kaplun
    Erik Kaplun about 10 years
    too bad git add -A -p doesn't work as one would expect (ask interactively about untracked files)
  • lawlist
    lawlist about 10 years
    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 ` :/` ?
  • Gabriel Llamas
    Gabriel Llamas almost 10 years
    Please update the answer. It should be: git add -A :/ or git add -A .
  • CB Bailey
    CB Bailey almost 10 years
    @GabrielLlamas: Please see here: stackoverflow.com/help/privileges/edit ; you should have sufficient privileges to make edits yourself.
  • Mazzone
    Mazzone almost 10 years
    Do Caps matter? (i.e. 'git add -A' vs. 'git add -a')
  • ajdeguzman
    ajdeguzman almost 10 years
    Hi @Mazzone, there's no such command as git add -a, caps matter
  • TheGrapeBeyond
    TheGrapeBeyond almost 10 years
    Hello, what if you just wanted to stage only modified files? How would you do that?
  • K. Kilian Lindberg
    K. Kilian Lindberg almost 10 years
    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 -> stackoverflow.com/questions/14368093/…
  • Brizee
    Brizee almost 10 years
    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.
  • Gokul N K
    Gokul N K over 9 years
    Thanks for the table. Is there a way to add only the files that were modified. No new files or deleted files
  • Ville
    Ville almost 9 years
    @Gokul: According to this post, 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.
  • VonC
    VonC almost 9 years
    @NickVolynkin That's great! Glad to see the international community of SO working as intended. For reference: ru.stackoverflow.com/a/431840
  • Milo Wielondek
    Milo Wielondek almost 9 years
    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.
  • Nick Volynkin
    Nick Volynkin almost 9 years
    Actually it's git add :/ + git add -u :/
  • Claudiu Creanga
    Claudiu Creanga over 8 years
    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
  • Pacerier
    Pacerier over 8 years
    @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
    Pacerier over 8 years
    @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
  • Dr.jacky
    Dr.jacky almost 8 years
    @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
    Dr.jacky over 7 years
    @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.
  • Neutrino
    Neutrino over 6 years
    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
    Neutrino over 6 years
    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.
  • Emile Bergeron
    Emile Bergeron over 6 years
    @yuan-ji be careful when editing to avoid removing critical information.
  • creator
    creator about 6 years
    What is git add * ? There are none of description.
  • flow2k
    flow2k about 6 years
    So, git add . is equivalent to git add -A ., which is equivalent to git add "*"
  • void.pointer
    void.pointer over 5 years
    This is wrong, git add . does add deleted files in your working copy. They get staged as deleted.
  • H S Umer farooq
    H S Umer farooq over 5 years
    I m still confuse about git add "*", can you please elaborate it little more?
  • xgqfrms
    xgqfrms over 4 years
    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
  • user85
    user85 over 3 years
    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}
  • mfaani
    mfaani about 3 years
    git add * doesn't add hidden files, while git add . & git add -A do add hidden files
  • Jacob Lee
    Jacob Lee about 3 years
    @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.
  • np8
    np8 about 3 years
    Is the answer for Git Version 1.x still relevant in 2021?
  • Spoderman4
    Spoderman4 almost 3 years
    This should be the top/selected/pinned answer. @cmcginty if you're still out there, after 12 years
  • Ryker
    Ryker over 2 years
    @Pacerier At least for React Native, I find git add -u to be super useful as new files get added by builds or apk generation and sometimes I want to only commit the non-generated files in the current commit to avoid clutter,
  • CervEd
    CervEd over 2 years
    git add -A is equivalent to git add :/; git add -u.