`git add .` 和 `git add -u` 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2190409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:47:10  来源:igfitidea点击:

What's the difference between `git add .` and `git add -u`?

gitgit-add

提问by TK.

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?
  • git add .和 和有git add -u什么区别?

回答by VonC

It is one of the git gotchas mentioned here(pre Git 2.0).

这是这里提到的 git 陷阱之一(Git 2.0 之前)。

git add .only adds what is there, not what has been deleted (if tracked).

git add .只添加现有内容,而不添加已删除的内容(如果已跟踪)。

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 -Awould take care of both steps...

git add -A会照顾两个步骤......



With Git 2.0, git add -Ais default.

对于Git 2.0,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 <path>git add -A <path>现在与“ ”相同,因此“ git add dir/”会注意到您从目录中删除的路径并记录删除。
在旧版本的 Git 中," git add <path>" 用于忽略删除。

如果您真的想要,您可以说“ git add --ignore-removal <path>”以仅在 中添加添加或修改的路径<path>



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

警告(git1.8.3 April 2013,对于即将推出的 git2.0)。
我修改了我的答案说git add -u ., 而不是git add -u.:

git add -uwill operate on the entire treein 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.

git add -u将在 Git 2.0 中对整个树进行操作,以与“ git commit -a”和其他命令保持一致。
因为没有机制让“ git add -u”表现得像“ git add -u .”,所以对于那些习惯于“ git add -u”(没有路径规范)只更新当前子目录中的路径的索引的人来说,开始训练他们的手指明确地说“ git add -u .”是很重要的当他们在 Git 2.0 到来之前是认真的。

As I mentioned in "e"

正如我在“ e”中提到的

回答by Benjamin Bannier

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.

就像手册说的:git add .将添加当前目录中的所有文件,而git add -u .只会添加那些已经被跟踪的文件。

回答by Derek Swingley

git add documentaiton

git 添加文档

git add . 

add all files from the current directory

添加当前目录中的所有文件

git add -u 

only update files currently being tracked.

仅更新当前正在跟踪的文件。