git: abort commit in the middle of typing message

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Solution 7 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander BirdView Question on Stackoverflow
Solution 1 - GitnimrodmView Answer on Stackoverflow
Solution 2 - GitBorealidView Answer on Stackoverflow
Solution 3 - GitzettaView Answer on Stackoverflow
Solution 4 - GitbdonlanView Answer on Stackoverflow
Solution 5 - GitRudolf TucekView Answer on Stackoverflow
Solution 6 - GitSovietFrontierView Answer on Stackoverflow
Solution 7 - GitDoron BeharView Answer on Stackoverflow