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

2018-12-31 02:23发布

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

标签: git git-add
10条回答
只靠听说
2楼-- · 2018-12-31 02:56

Things changed with Git 2.0:

  • -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
查看更多
大哥的爱人
3楼-- · 2018-12-31 02:58

In Git 2.x:

  • If you are located directly at the working directory, then git add -A and git add . work without 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.

查看更多
皆成旧梦
4楼-- · 2018-12-31 03:02

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

git add 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.

查看更多
笑指拈花
5楼-- · 2018-12-31 03:03

So from Charles instructions above, after testing my proposed understanding would be as follow:

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

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

查看更多
忆尘夕之涩
6楼-- · 2018-12-31 03:16

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

In GIT 2 the -A option is now 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

查看更多
只若初见
7楼-- · 2018-12-31 03:17

Here is table for quick understanding:

Git Version 1.x: enter image description here

Git Version 2.x: enter image description here

Long-form flags:

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

Further reading:

查看更多
登录 后发表回答