git commit -am 是多余的,如果我之前做过 git add 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3629545/
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
Is git commit -am redundant, if I do git add before?
提问by Lee McAlilly
Is it redundant to run git add .
and then git commit -am "commit message"
?
git add .
然后运行是多余的git commit -am "commit message"
吗?
Can I just run git add .
and then git commit -m "commit message"
or, alternatively, just git commit -am "commit message"
?
我可以运行git add .
然后git commit -m "commit message"
,或者,只是git commit -am "commit message"
?
回答by Tuminoid
Git add
+ git commit -m
will just commit those files you've added (new and previously tracked), but git commit -am
will commit all changes on tracked files, but it doesn't add new files.
Git add
+git commit -m
只会提交您添加的那些文件(新的和以前跟踪的),但git commit -am
会提交对跟踪文件的所有更改,但不会添加新文件。
回答by Karl Horky
With Git before version 2.0, it's not redundant to run git commit -am
if you deleted any tracked files. See further explanation of the options below.
使用 2.0 版之前的 Git,git commit -am
如果您删除了任何跟踪的文件,运行也不是多余的。请参阅以下选项的进一步说明。
As long as you're in the root directory, and you want to add all new files, modifications and deletions, you want 3)below.
只要您在根目录中,并且想要添加所有新文件、修改和删除,您就需要下面的3)。
1) Commit new files and modifications to previously-tracked files, but don't add deletions.
1) 提交新文件和对以前跟踪的文件的修改,但不要添加删除。
With Git < 2.0:
使用 Git < 2.0:
$ git add .
$ git commit -m
With Git >= 2.0:
使用 Git >= 2.0:
$ git add --ignore-removal .
$ git commit -m
2) Commit modifications and deletions to tracked files, but don't add new files.
2) 对跟踪的文件进行修改和删除,但不要添加新文件。
$ git commit -am
3) Commit new files and all changed tracked files (both modifications and deletions). Either:
3) 提交新文件和所有更改的跟踪文件(修改和删除)。任何一个:
With Git < 2.0:
使用 Git < 2.0:
$ git add .
$ git commit -am
...Or:
...或者:
$ git add -A
$ git commit -m
With Git >= 2.0:
使用 Git >= 2.0:
$ git add .
$ git commit -m
回答by xZise
Actually, git commit -a
is not always redundant, as it also removing files! For example you have two files: One file is tracked the other not. Now you want to delete the tracked (everywhere) and add the not tracked.
实际上,git commit -a
并不总是多余的,因为它还会删除文件!例如,您有两个文件:一个文件被跟踪,另一个文件不被跟踪。现在您要删除已跟踪的(无处不在)并添加未跟踪的。
So you remove the tracked file. If you then use only git add .
and then git commit
it will track the new but won't delete the old file in the repository.
所以你删除了被跟踪的文件。如果您只使用git add .
然后git commit
它会跟踪新文件但不会删除存储库中的旧文件。
So you either execute git rm …
or using git commit -a
.
所以你要么执行git rm …
要么使用git commit -a
.
回答by Daniel Stutzbach
I think some of the previous answers did not see the period after git add
(your original question and some of the answers have since been edited to make the period more clear).
我认为之前的一些答案没有看到之后的句点git add
(您的原始问题和一些答案已被编辑以使句号更加清晰)。
git add .
will add all files in the current directory (and any subdirectories) to the index (except those being ignored by .gitignore
).
git add .
会将当前目录(和任何子目录)中的所有文件添加到索引中(被 忽略的文件除外.gitignore
)。
The -a
option to git commit
will include all changes to files that are already being tracked by git, even if those changes have not been added to the index yet.
该-a
选项git commit
将包括那些已经被git的跟踪的所有文件的更改,即使这些变更尚未添加到索引呢。
Consequently, if all of the files are already being tracked by git, then the two approaches have the same effect. On the other hand, if new files have been created, then git add .; git commit
will add them to the repository, while git commit -a
will not.
因此,如果所有文件都已被 git 跟踪,则这两种方法具有相同的效果。另一方面,如果创建了新文件,git add .; git commit
则将它们添加到存储库中,而git commit -a
不会。
git add .; git commit -a
is indeed redundant. All changes have already been added to the index, so the -a
does nothing.
git add .; git commit -a
确实是多余的。所有更改都已添加到索引中,因此-a
什么都不做。
回答by kashif
The -a
switch tells git to stage the modified files for adding - it stands for 'all' not 'add'. If you wan't the content in a new file to be added to git then you must add it using git add before you commit it.
该-a
开关告诉 git 暂存修改后的文件以进行添加 - 它代表“全部”而不是“添加”。如果您不想将新文件中的内容添加到 git 中,那么您必须在提交之前使用 git add 添加它。
If you have already done a git add .
then it has already added all files in the current directory i.e. both new and modified files. Hence you don't need a -a
switch to stage content again. So you can rightly just follow it up with a git -m "msg"
如果您已经完成了,git add .
那么它已经添加了当前目录中的所有文件,即新文件和修改过的文件。因此,您不需要-a
再次切换到舞台内容。所以你可以正确地跟随它git -m "msg"