git: abort commit in the middle of typing message

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.


Solution 1:

If your editor can exit with an error code -- Git will abort the commit. When using VIM, type

:cq

to exit with an non-zero error code and abort the commit.

Solution 2:

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

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.

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.