Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: abort commit in the middle of typing message

Tags:

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.


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.


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.

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

  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.