Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Recover failed commit's message

Tags:

git

vim

Every now and then, as I am dutifully crafting a nice, descriptive commit message, I get an error:

".git/COMMIT_EDITMSG" 81L, 2108C written
error: There was a problem with the editor 'vim'.
Please supply the message using either -m or -F option.

Note this is usually after :wq. I check the .git/COMMIT_EDITMSG file and it has no changes in it. Is there another place that git saves this message to so I might recover it and try committing again? Do people have experience with this problem and know why it might be happening to me? I have no issues writing to other files and permissions seem to be in order.

like image 903
devoid Avatar asked Feb 03 '12 18:02

devoid


2 Answers

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]
like image 105
JoseKilo Avatar answered Oct 27 '22 11:10

JoseKilo


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.

like image 27
kdev Avatar answered Oct 27 '22 10:10

kdev