git:在输入消息的过程中中止提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9146907/
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: abort commit in the middle of typing message
提问by Alexander Bird
I am in the middle of committing. I have typed up my commit message in vim. I now remembered I needed to change something. I realize that there are other options to accomplish what I want, but I want to know if there is a way to abort the commit but still save the commit message I've typed up so far.
我正在提交中。我已经在 vim 中输入了我的提交信息。我现在记得我需要改变一些东西。我意识到还有其他选项可以完成我想要的,但我想知道是否有办法中止提交但仍然保存我到目前为止输入的提交消息。
采纳答案by Borealid
Yes. Write the commit message to a different file (:w /some/other/path.txt
). Then exit the editor without saving (:q!
). If you previously saved the file to its original path, delete everything and write the empty file first (an empty commit message will abort the commit).
是的。将提交消息写入不同的文件 ( :w /some/other/path.txt
)。然后退出编辑器而不保存 ( :q!
)。如果您之前将文件保存到其原始路径,请删除所有内容并首先写入空文件(空提交消息将中止提交)。
Now, when you're ready to commit "for reals", use the message file you saved at the alternate path.
现在,当您准备好提交“实数”时,请使用您保存在备用路径中的消息文件。
Alternately, copy the commit message into one of vim's buffers.
或者,将提交消息复制到 vim 的缓冲区之一。
It's worth noting that you really don't have to do this at all: commit --amend
lets you alter the commit after it's made, so the easy solution is to produce the commit with what you've got and then fix it before pushing. You can even just finish the commit in its broken state, reset HEAD~
(resets you to the state your working copy was in before the commit), fix your working copy, and then commit -a -c HEAD@{1}
to use the old commit message.
值得注意的是,您实际上根本不必这样做:commit --amend
让您在提交后更改提交,因此简单的解决方案是使用您拥有的内容生成提交,然后在推送之前修复它。您甚至可以在其损坏状态下完成提交reset HEAD~
(将您重置为您的工作副本在提交之前所处的状态),修复您的工作副本,然后commit -a -c HEAD@{1}
使用旧的提交消息。
回答by nimrodm
If your editor can exit with an error code -- Git will abort the commit. When using VIM, type
如果您的编辑器可以退出并显示错误代码——Git 将中止提交。使用 VIM 时,键入
:cq
to exit with an non-zero error code and abort the commit.
以非零错误代码退出并中止提交。
回答by zetta
If you have vim opened to write your commit message just delete the lines that don't start with #and git will abort your commit
如果你打开 vim 来写你的提交信息,只需删除不以#开头的行,git 将中止你的提交
Aborting commit due to empty commit message.
回答by bdonlan
While you can abort the commit, another approach is to amendthe commit afterward. Simply commit your current work, then make whatever additional changes you want, git add
them, then run git commit --amend
. You'll be placed back into the commit message editor, and when you save, the commit will be amended to include the additional changes and your new commit message.
虽然您可以中止提交,但另一种方法是在之后修改提交。只需提交您当前的工作,然后进行您想要的任何其他更改git add
,然后运行git commit --amend
. 您将被放回到提交消息编辑器中,当您保存时,提交将被修改以包括其他更改和您的新提交消息。
回答by Rudolf Tucek
Yes it's possible. In order to commit, your editor MUST write the commit message to the file .git/COMMIT_EDITMSG
and exit with a 0 status code.
是的,这是可能的。为了提交,您的编辑器必须将提交消息写入文件.git/COMMIT_EDITMSG
并以 0 状态代码退出。
So if you're using VI/VIM, you may want to do the following...
因此,如果您正在使用 VI/VIM,您可能需要执行以下操作...
- Write your commit message.
- Save the commit message with
:w
(by default this will save the current content to.git/COMMIT_EDITMSG
) - Exit the editor with an error
:cq
- ...do what you have to do... even add/remove files to staging area.
- Continue with editing your existing commit message with
git commit -eF .git/COMMIT_MESSAGE
- 写下你的提交信息。
- 保存提交消息
:w
(默认情况下,这会将当前内容保存到.git/COMMIT_EDITMSG
) - 退出编辑器并出现错误
:cq
- ...做你必须做的事情...甚至将文件添加/删除到暂存区。
- 继续编辑现有的提交消息
git commit -eF .git/COMMIT_MESSAGE
The -F /path/to/file
will populate the editor with any given content from /path/to/file. However, by default this would instantly perform the commit, unless you provide the -e
flag additionally for editing.
在-F /path/to/file
将填充与/路径/任何给定的内容/文件编辑器。但是,默认情况下,这将立即执行提交,除非您-e
额外提供用于编辑的标志。
回答by SovietFrontier
I usually make my commits within IntelliJ
using the terminals provided. Unfortunately my commits are manual:
我通常IntelliJ
使用提供的终端进行提交。不幸的是,我的提交是手动的:
git commit -m'my message' // etc
git commit -m'my message' // etc
so the only thing I think that is safe to do is just close the terminal window!Just highlight your text if you want to save it, then copy, then close the terminal window.
所以我认为唯一可以安全做的就是关闭终端窗口!如果要保存,只需突出显示文本,然后复制,然后关闭终端窗口。
回答by Doron Behar
@bdonlan answer is good for this very question but I'll point out to a situation where you might want a better solution.
@bdonlan 的答案非常适合这个问题,但我会指出您可能需要更好解决方案的情况。
Say you want to add changes to last commit. So you do as @bdolan suggested:
假设您想将更改添加到上次提交。所以你按照@bdolan 的建议去做:
git add files
git commit --amend
Imagine that during writing the new message, You regret adding those files to that commit. The problem isyou are stuck with an already savedcommit message and exiting the editor (with or without saving) will add those changes to the last commit. Reverting back to the point you were at before these actions requires you to split the last commit- I'd bet you want to avoid it.
想象一下,在编写新消息的过程中,您后悔将这些文件添加到该提交中。问题是你被一个已经保存的提交消息卡住了,退出编辑器(保存或不保存)会将这些更改添加到最后一次提交。恢复到您在这些操作之前所处的位置需要您拆分最后一次提交- 我敢打赌您想避免它。
The trick is to save and exit the editor while it has only lines starting with #
or no lines at all. When you exit you will be greeted with the message:
诀窍是保存并退出编辑器,而它只有行开头#
或根本没有行。当您退出时,您将收到以下消息:
Aborting commit due to empty commit message.
And you haven't changed the last commit at all.
而且您根本没有更改最后一次提交。