Git 提交而无需暂存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46221861/
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
Git commit without staging
提问by Stef
I noticed that git allows to commit a tracked file (which was modified locally) directly without using staging, eg:
我注意到 git 允许在不使用暂存的情况下直接提交跟踪文件(在本地修改),例如:
$ git status -s .
M myfile.txt
$ git commit myfile.txt -m"1 way"
Is it better to use, instead, the "classic" two steps ? :
使用“经典”两步法是否更好?:
$ git add myfile.txt
$ git commit myfile.txt -m"2 way"
回答by axiac
Is it better to use, instead, the "classic" two steps ?
使用“经典”两步法是否更好?
The documentation of git commit
mentions no less than 5ways to tell Git what you want to include in the next commit:
文档中git commit
提到不少于5种方法来告诉 Git 你想在下一次提交中包含什么:
The content to be added can be specified in several ways:
- by using
git add
to incrementally "add" changes to the index before using the commit command (Note: even modified files must be "added");- by using
git rm
to remove files from the working tree and the index, again before using the commit command;- by listing files as arguments to the commit command (without
--interactive
or--patch
switch), in which case the commit will ignore changes staged in the index, and instead record the current content of the listed files (which must already be known to Git);- by using the
-a
switch with the commit command to automatically "add" changes from all known files (i.e. all files that are already listed in the index) and to automatically "rm" files in the index that have been removed from the working tree, and then perform the actual commit;- by using the
--interactive
or--patch
switches with the commit command to decide one by one which files or hunks should be part of the commit in addition to contents in the index, before finalizing the operation. See the "Interactive Mode" section ofgit-add
to learn how to operate these modes.
可以通过多种方式指定要添加的内容:
- 通过使用
git add
以增量使用commit命令之前“添加”改变到索引(注:甚至修改的文件必须是“添加”);- 通过
git rm
删除从工作树和索引文件,再次使用前提交命令;- 通过列出文件作为提交命令的参数(不带
--interactive
或--patch
开关),在这种情况下,提交将忽略索引中的更改,而是记录列出文件的当前内容(Git 必须已经知道);- 通过使用
-a
带有 commit 命令的开关自动“添加”所有已知文件(即索引中已列出的所有文件)的更改,并自动“rm”索引中已从工作树中删除的文件,以及然后执行实际提交;- 在完成操作之前,通过使用
--interactive
or--patch
开关和 commit 命令来一一决定除了索引中的内容之外哪些文件或大块应该成为提交的一部分。请参阅“交互模式”部分git-add
以了解如何操作这些模式。
There are so many options not because some of them are "good" while others are "better". Git provides so many options to let youpick the one you prefer or the one that fits best in a specific situation.
有这么多选择,并不是因为其中一些“好”而另一些“更好”。Git 提供了许多选项,让您可以选择自己喜欢的选项或最适合特定情况的选项。
For example, if you discover there is a modified file that should be part of the previous commit (but you forgot to commit it) and the index currently contains the files prepared for the next commit, using the first command from the question (option #3 in the list above) allows you to commit only that file without changing the index. This way the file you missed skips the line and is committed where it belongs, right after the previous commit, without interfering with the next commit you are preparing.
例如,如果您发现有一个修改过的文件应该是上一次提交的一部分(但您忘记提交它),并且索引当前包含为下一次提交准备的文件,请使用问题中的第一个命令(选项 #上面列表中的第 3 个)允许您仅提交该文件而不更改索引。这样,您错过的文件会跳过该行并在上一次提交之后立即提交到它所属的位置,而不会干扰您正在准备的下一次提交。
回答by Ivan Choo
Try git commit -a -m [message]
尝试 git commit -a -m [message]
-a
-一种
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.
告诉命令自动暂存已修改和删除的文件,但您没有告诉 Git 的新文件不受影响。
Doesn't work for new files, you still need to explicitly add them.
不适用于新文件,您仍然需要明确添加它们。
It's just stage followed by commit in a single step. I find this convenient since one can see all changed states when using status
and we typically make small incremental commits.
它只是一个阶段,然后一步提交。我发现这很方便,因为在使用时可以看到所有更改的状态,status
并且我们通常会进行少量的增量提交。