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.
8 Answers
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.

- 23,081
- 7
- 58
- 59
-
7This is a nice trick for aborting a `commit --amend` as you would otherwise have to delete the existing commit message and `:wq`. – Alex Jasmin Oct 08 '15 at 00:28
-
3Nice trick! However the question was how to save the commit message, not delete it. – Elijah Lynn Dec 17 '15 at 13:55
-
so how does svn manage to do this? what does it look at besides exit code? – Chaim Geretz May 26 '16 at 22:12
-
1great when you're doing an interactive rebase aka squashing commits – hellatan Dec 01 '16 at 18:55
-
saved my life when I did a `git commit --amend` instead of `git rebase --continue` during rebase. – Weishi Z Jun 11 '18 at 18:56
-
This should be the answer, combined with @Borealid's advice of saving the commit message, if you just typed it and don't want to lose it. – automorphic Sep 30 '22 at 16:22
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.

- 61,572
- 58
- 208
- 243

- 95,191
- 9
- 106
- 122
-
10Note: Quiting vim using ":q!" won't abort the commit if there is an autopopulated merge conflict message. Instead the commit will continue and the message will not list which files had conflicts. – stephenbez Oct 17 '16 at 22:03
-
2`write the empty file (an empty commit message will abort the commit)` part worked like a charm, thanks! – Sasha Davydenko Nov 22 '19 at 15:43
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
Aborting commit due to empty commit message.

- 944
- 6
- 4
-
16This is particularly useful when you want to abort a git commit --amend. – edsko Sep 21 '14 at 09:40
-
1The question was how to abort the commit but save the commit message. Deleting lines that don't start with # effectively deletes the commit message, not saves the commit message. Not sure how this has so many upvotes. – Elijah Lynn Dec 17 '15 at 13:54
-
3`ggdG` will cut all the content of the message, then next time you run `vim` you can just `P` the text you previously cut. – oromoiluig Aug 31 '18 at 13:41
-
1
While you can abort the commit, another approach is to amend the 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.

- 224,562
- 31
- 268
- 324
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.
So if you're using VI/VIM, you may want to do the following...
- 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
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.

- 349
- 4
- 10
-
this works, but is less preferable because 1) it assumes I the CWD is where the .git folder is 2) I use [git worktrees](https://git-scm.com/docs/git-worktree), and with git worktrees, .git is actually a _file_ saying where the real .git folder is. – Alexander Bird Jan 02 '17 at 20:47
-
@AlexanderBird this is not true... when you want to pick up the old commit message in step 5), you'd just have to specify a relative path (say you're in the `/foo/bar/` directory, relative from the projects root) like `git commit -eF ../../.git/COMMIT_MESSAGE` – Rudolf Tucek Jan 05 '17 at 05:24
-
@AlexanderBird cool, haven't yet noticed with feature of multiple working trees in git. Thx for the hint! I believe the only downside on this is, you'd have to keep a general overview in your head, which prevents you from focusing on the actual code. And just for picking up the commit message on "where I've left off` it's a bit heavy :) – Rudolf Tucek Jan 05 '17 at 05:29
-
1@AlexanderBird then instead of `:w` do `:sav committmp` and `git commit -eF committmp` – Hashbrown Sep 12 '19 at 10:44
A simple way to do that is to just put an empty commit message(with or without comments as they don't count).
Will work with all text editors. The commit will fail saying -
Aborting commit due to empty commit message.

- 99
- 1
- 9
-
1The intent of my question is to see if there's a way to save my message so far. While the most functional goal is accomplished with this suggested answer, my question, I think, was specific about wanting to save typed message somewhere to re-use when committing again later. – Alexander Bird Jul 22 '22 at 17:43
I usually make my commits within IntelliJ
using the terminals provided. Unfortunately my commits are manual:
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.

- 2,047
- 1
- 15
- 33
@bdonlan answer is good for this very question but I'll point out to a situation where you might want a better solution.
Say you want to add changes to last commit. So you do as @bdolan suggested:
git add files
git commit --amend
Imagine that during writing the new message, You regret adding those files to that commit. The problem is you are stuck with an already saved commit 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.

- 1
- 1

- 2,606
- 2
- 21
- 24
-
If I understand what you're saying is a problem, then you're incorrect; as demonstrated here: `git commit; vim ....; git commit --amend; git reset HEAD@{1}`. That is: if I want to "undo" the amendment, then I do _not_ have to split previous commit. the commit object which was HEAD prior to amend is saved as `HEAD@{1}`. Also, zetta already suggested deleting all non comment lines. – Alexander Bird Jun 06 '16 at 02:09