“git add -A”和“git add”之间的区别。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/572549/
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
Difference between "git add -A" and "git add ."
提问by cmcginty
The command git add [--all|-A]
appears to be identical to git add .
. Is this correct? If not, how do they differ?
该命令git add [--all|-A]
似乎与git add .
. 这样对吗?如果不是,它们有什么不同?
回答by CB Bailey
This answer only applies to Git version 1.x. For Git version 2.x, see other answers.
此答案仅适用于Git 版本 1.x。对于 Git 版本 2.x,请参阅其他答案。
Summary:
概括:
git add -A
stages all changesgit add .
stages new files and modifications, without deletionsgit add -u
stages modifications and deletions, without new files
git add -A
阶段性的所有变化git add .
暂存新文件和修改,不删除git add -u
阶段修改和删除,没有新文件
Detail:
细节:
git add -A
is equivalent to git add .; git add -u
.
git add -A
相当于 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 .
是,它查看工作树并将所有这些路径添加到暂存的更改中,如果它们已更改或为新的且未被忽略,则它不会暂存任何“rm”操作。
git add -u
looks at all the alreadytracked 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 -u
查看所有已跟踪的文件,并暂存对这些文件的更改(如果它们不同或已被删除)。它不会添加任何新文件,它只会对已跟踪文件进行更改。
git add -A
is a handy shortcut for doing both of those.
git add -A
是执行这两项操作的便捷快捷方式。
You can test the differences out with something like this (note that for Git version 2.x your output for git add .
git status
willbe different):
您可以使用类似的方法测试差异(请注意,对于 Git 版本 2.x,您的输出git add .
git status
会有所不同):
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
回答by Developer
Here is a table for quick understanding:
这是一个快速理解的表格:
Git Version 1.x:
Git 版本 1.x:
Git Version 2.x:
Git 版本 2.x:
Long-form flags:
长格式标志:
git add -A
is equivalent togit add --all
git add -u
is equivalent togit add --update
git add -A
相当于git add --all
git add -u
相当于git add --update
Further reading:
进一步阅读:
回答by VonC
With Git 2.0, git add -A
is default: git add .
equals git add -A .
.
使用Git 2.0,git add -A
默认为:git add .
等于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 <path>
git add -A <path>
现在与“ ”相同,因此“git add dir/
”会注意到您从目录中删除的路径并记录删除。
在旧版本的 Git 中,"git add <path>
" 忽略了删除。如果您真的想要,您可以说“
git add --ignore-removal <path>
”以仅在 中添加添加或修改的路径<path>
。
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
).
git add -A
就像git add :/
(从顶部 git repo 文件夹添加所有内容)。
请注意,git 2.7(2015 年 11 月)将允许您添加名为“ :
”的文件夹!
请参阅Junio C Hamano() 的commit 29abb33(2015 年 10 月 25 日)。 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.
请注意,从 git 2.0 (Q1 or Q2 2014) 开始,在谈论git add .
(工作树中的当前路径)时,您也必须.
在其他git add
命令中使用“ ” 。
That means:
这意味着:
"
git add -A .
" is equivalent to "git add .; git add -u .
"
“
git add -A .
”相当于“git add .; git add -u .
”
(Note the extra '.
' for git add -A
and git add -u
)
(注意额外的 ' .
'git add -A
和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.
因为git add -A
orgit add -u
会在整个工作树上运行(仅从 git 2.0 开始),而不仅仅是在当前路径上。
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.
这些命令将在 Git 2.0 中的整个树上运行,以与 "
git commit -a
" 和其他命令保持一致。因为不会有机制,使“git add -u
”表现得好像“git add -u .
”,它是为那些谁是用来“重要git add -u
”(不pathspec)仅适用于在当前子目录路径,开始他们的手指培训,明确说更新索引“git add -u .
“当他们在 Git 2.0 出现之前就意味着它。当这些命令在没有路径规范的情况下运行并且在当前目录之外有本地更改时会发出警告,因为在这种情况下 Git 2.0 中的行为将与今天的版本不同。
回答by Richard
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.
这篇博文也可能有助于了解在什么情况下可以应用这些命令:从 Git 工作目录中删除已删除的文件。
回答by 0xF
Things changedwith Git 2.0 (2014-05-28):
Git 2.0(2014-05-28)发生了变化:
-A
is now the default- The old behavior is now available with
--ignore-removal
. git add -u
andgit add -A
in a subdirectory without paths on the command line operate on the entire tree.
-A
现在是默认设置- 旧行为现在可用于
--ignore-removal
. git add -u
并且git add -A
在命令行上没有路径的子目录中对整个树进行操作。
So for Git 2 the answer is:
所以对于 Git 2,答案是:
git add .
andgit add -A .
add new/modified/deleted files in the current directorygit add --ignore-removal .
adds new/modified files in the current directorygit add -u .
adds modified/deleted files in the current directory- Without the dot, add all files in the project regardless of the current directory.
git add .
并git add -A .
在当前目录中添加新的/修改的/删除的文件git add --ignore-removal .
在当前目录中添加新的/修改过的文件git add -u .
在当前目录中添加修改/删除的文件- 如果没有点,则添加项目中的所有文件,而不考虑当前目录。
回答by K. Kilian Lindberg
A more distilled quick answer:
一个更精炼的快速答案:
Does both below (same as git add --all)
是否在下面(与git add --all相同)
git add -A
Stages new + modified files
暂存新文件 + 修改文件
git add .
Stages modified + deleted files
阶段修改+删除文件
git add -u
回答by simhumileco
In Git 2.x:
在Git 2.x 中:
If you are located directly at the working directory, then
git add -A
andgit 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, andgit add .
will add files from your current directory.
如果您直接位于工作目录,则
git add -A
和git add .
工作没有区别。如果您在工作目录的任何子目录中,
git add -A
将添加整个工作目录中的所有文件,git add .
并将添加您当前目录中的文件。
And that's all.
就这样。
回答by AnneTheAgile
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.
限制器可以是 -u 或 -A 或 nil。
Pathspec may be a filepath or dot, '.' to indicate the current directory.
Pathspec 可以是文件路径或点,'.' 来指示当前目录。
Important background knowledge about how Git 'adds':
关于 Git 如何“添加”的重要背景知识:
- 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.
- Git 永远不会自动识别不可见文件,即那些以点为前缀的文件(点文件)。它们甚至从未被列为“未跟踪”。
- Git 永远不会添加空文件夹。它们甚至从未被列为“未跟踪”。(解决方法是向跟踪的文件添加一个可能不可见的空白文件。)
- Git status 不会显示子文件夹信息,即未跟踪的文件,除非该子文件夹中至少有一个文件被跟踪。在此之前,Git 认为整个文件夹超出范围,即“空”。它是空的跟踪项目。
- 指定文件规范 = '.' (dot) 或当前目录不是递归的,除非
-A
另外指定。点严格指的是当前目录 - 它省略了上面和下面找到的路径。
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. => ifthe 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. => ifthe item is recognized,git add -A
will find it and add it.
-u
=--update
= 跟踪文件的子集 => 添加 = 否;改变 = 是; 删除 = 是。=>如果项目被跟踪。-A
=--all
(no such-a
,这会导致语法错误) = 所有未跟踪/跟踪文件的超集,除非在 2.0 之前的 Git 中,其中如果给出了点文件规范,则仅考虑该特定文件夹。=>如果该项目被识别,git add -A
将找到它并添加它。
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);
- 在 2.0 之前的 Git 中,对于两个限制器(update 和 all),新的默认是操作整个工作树,而不是当前路径(Git 1.9 或更早版本),
- 但是,在 v2.0 中,可以将操作限制在当前路径:只需添加显式点后缀(这在 Git 1.9 或更早版本中也有效);
git add -A .
git add -A .
git add -u .
git add -u .
In conclusion, my policy is:
总之,我的政策是:
- Ensure any hunks/files to be added are accounted for in
git status
. - If any items are missing, due to invisible files/folders, add them separately.
- Have a good
.gitignore
file so that normally only files of interest are untracked and/or unrecognized. - From the top level of the repository, "git add -A" to add all items. This works in all versions of Git.
- Remove any desired items from the index if desired.
- If there is a big bug, do 'git reset' to clear the index entirely.
- 确保要添加的任何大块/文件都在
git status
. - 如果缺少任何项目,由于文件/文件夹不可见,请单独添加它们。
- 拥有一个好的
.gitignore
文件,以便通常只有感兴趣的文件未被跟踪和/或无法识别。 - 从存储库的顶层,“git add -A”添加所有项目。这适用于所有版本的 Git。
- 如果需要,从索引中删除任何所需的项目。
- 如果有大错误,请执行 'git reset' 以完全清除索引。
回答by Alex78191
git add .
equals git add -A .
adds files to index only from current and children folders.
git add .
equalsgit add -A .
仅将文件从当前文件夹和子文件夹添加到索引。
git add -A
adds files to index from all folders in working tree.
git add -A
将文件从工作树中的所有文件夹添加到索引。
P.S.: information relates to Git 2.0(2014-05-28).
PS:信息与Git 2.0(2014-05-28) 有关。
回答by sbr_amd
Both git add .
and git add -A
will stage all new, modified and deleted files in the newer versions of Git.
双方git add .
并git add -A
会阶段的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 (notthe files lying outside, i.e., higher directories).
不同之处在于,git add -A
在属于您工作的 Git 存储库的“更高、当前和子目录”中暂存文件。但是git add .
只执行当前目录及其后的子目录中的文件(而不是位于外部的文件,即更高的目录)。
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 notstage the deleted file. But doing git add -A
will certainly stage this change no matter where you perform the command from.
如果您当前的工作目录是/my-repo
,并且您是rm rootfile.txt
,然后是cd subfolder
,然后是git add .
,那么它不会暂存已删除的文件。但是git add -A
无论您从哪里执行命令,这样做肯定会进行这种更改。