162

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 8

235

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.

6
  • 7
    This 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
  • 3
    Nice 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
    great when you're doing an interactive rebase aka squashing commits
    – hellatan
    Dec 1, 2016 at 18:55
  • saved my life when I did a git commit --amend instead of git rebase --continue during rebase.
    – Weishi Z
    Jun 11, 2018 at 18:56
96

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.

2
  • 10
    Note: 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, 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
81

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.
4
  • 16
    This is particularly useful when you want to abort a git commit --amend.
    – edsko
    Sep 21, 2014 at 9:40
  • 1
    The 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 run vim you can just P the text you previously cut.
    – oromoiluig
    Aug 31, 2018 at 13:41
  • 1
    works using nano, ctrl+k all lines then save and exit will abort
    – Juh_
    Jun 18, 2020 at 17:34
12

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.

7

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

  1. Write your commit message.
  2. Save the commit message with :w (by default this will save the current content to .git/COMMIT_EDITMSG)
  3. Exit the editor with an error :cq
  4. ...do what you have to do... even add/remove files to staging area.
  5. 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.

4
  • 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) like git 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 and git commit -eF committmp
    – Hashbrown
    Sep 12, 2019 at 10:44
4

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.

1
  • 1
    The 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
0

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

@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
  • 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. Jun 6, 2016 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.