Git: Recover failed commit's message

Not sure as far as git on the COMMIT_EDITMSG when you're at this state. As mentioned earlier, you may be able to see if vim saved it. But my understanding is that vim's default is to delete the backup unless you've explicitly told it to keep them. In addition, if you didn't want to have these files scattered all over your directories, you can specify a directory to put them in (you may have to manually create the directory first).

Try adding the following two lines to your ~/.vimrc file:

 backup
 backupdir=~/.vim/backup

Manually create the ~/.vim/backup directory, then edit a file and exit. You should see a copy of the file with a "~" at the end of the name in your backup dir.

On a side note, if you're as lazy as I am, use ":x" to exit vim instead of ":wq". The ":x" does both a write and a quit.


I was able to solve this combining different solutions and using git only (without depending on vim or its config).

In my case I'm also using a repository with submodules, which makes it slightly different:

Instead of .git/.COMMIT_EDITMSG The message is stored in .git/modules/{REPO}/COMMIT_EDITMSG

Luckily, we can use git rev-parse --git-dir to tell us that.

And we can use git commit -eF [FILE] to take the commit message from a file (-F) and to edit the message (-e).

All together:

git commit -eF $(git rev-parse --git-dir)/COMMIT_EDITMSG

Since that's too long, we can also define an alias

git config --global alias.recommit '!git commit -eF $(git rev-parse --git-dir)/COMMIT_EDITMSG'

So that we can call it like:

git recommit [... other commit arguments]

Tags:

Vim

Git