Categories
git git-add

Difference between “git add -A” and “git add .”

3291

What is the difference between git add [--all | -A] and git add .?

0

    4703

    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
    

    21

    • 51

      How about the difference between git add *?

      – Jared

      Mar 24, 2012 at 0:52

    • 3

      too bad git add -A -p doesn’t work as one would expect (ask interactively about untracked files)

      Feb 15, 2014 at 22:52

    • 2

      Please update the answer. It should be: git add -A :/ or git add -A .

      May 4, 2014 at 8:19

    • 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 5, 2014 at 9:49

    • 19

      @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, 2015 at 10:15


    1229

    Git Version 1.x

    CommandNew FilesModified FilesDeleted FilesDescription
    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

    CommandNew FilesModified FilesDeleted FilesDescription
    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:

    7

    • 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, 2015 at 12:43

    • 3

      @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.

      – Ville

      May 8, 2015 at 5:41


    • 116

      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.

      Jul 6, 2015 at 12:44

    • 3

      So, git add . is equivalent to git add -A ., which is equivalent to git add "*"

      – flow2k

      Mar 25, 2018 at 23:21

    • 1

      This should be the top/selected/pinned answer. @cmcginty if you’re still out there, after 12 years

      Jul 5, 2021 at 15:14

    200

    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.

    2

    • 3

      @NickVolynkin That’s great! Glad to see the international community of SO working as intended. For reference: ru.stackoverflow.com/a/431840

      – VonC

      Jun 26, 2015 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, 2015 at 10:06