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.
-
7This is a nice trick for aborting a
commit --amend
as you would otherwise have to delete the existing commit message and:wq
. Oct 8, 2015 at 0:28 -
3Nice trick! However the question was how to save the commit message, not delete it. Dec 17, 2015 at 13:55
-
so how does svn manage to do this? what does it look at besides exit code? May 26, 2016 at 22:12
-
1
-
saved my life when I did a
git commit --amend
instead ofgit rebase --continue
during rebase.– Weishi ZJun 11, 2018 at 18:56
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.
-
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. Oct 17, 2016 at 22:03
-
2
write the empty file (an empty commit message will abort the commit)
part worked like a charm, thanks! Nov 22, 2019 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.
-
16This is particularly useful when you want to abort a git commit --amend.– edskoSep 21, 2014 at 9: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. Dec 17, 2015 at 13:54
-
3
ggdG
will cut all the content of the message, then next time you runvim
you can justP
the text you previously cut. Aug 31, 2018 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.
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.
-
this works, but is less preferable because 1) it assumes I the CWD is where the .git folder is 2) I use git worktrees, and with git worktrees, .git is actually a file saying where the real .git folder is. Jan 2, 2017 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) likegit commit -eF ../../.git/COMMIT_MESSAGE
Jan 5, 2017 at 5: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 :) Jan 5, 2017 at 5:29
-
1@AlexanderBird then instead of
:w
do:sav committmp
andgit commit -eF committmp
Sep 12, 2019 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.
-
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. Jul 22, 2022 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.
@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.
-
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 asHEAD@{1}
. Also, zetta already suggested deleting all non comment lines. Jun 6, 2016 at 2:09