29

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.

devoid
  • 1,009
  • 10
  • 16
  • Have you tried looking for vim's backup file? – freitass Feb 03 '12 at 18:33
  • 1
    that would be .git/.COMMIT_EDITMSG.swp? Yea, no luck there. – devoid Feb 03 '12 at 19:19
  • 7
    I don't have a solution for recovering the commit message, but I might know why you're getting the error. I see this behavior on my mac at work, but not on Linux at home. If I accidentally type ":Wq" when trying to save my commit message, when I go back and type ":wq" the commit fails. – haydenmuhl Feb 03 '12 at 21:35
  • 3
    I noticed this happens sometimes if I attempt to sign a git commit (I default to `git commit -S` and my USB key isn't inserted. In this situation, `.git/COMMIT_EDITMSG` does exist, and you can retrieve the message there. I'm not 100% sure why it doesn't automatically restore it to vim after I plug in my key and try `git commit -S` again, though... – jwir3 Apr 13 '17 at 19:57
  • 1
    as @haydenmuhl mentioned it's `:Wq` to blame. Setting `git config --global core.editor vim -f` as explained in [this SO answer](https://stackoverflow.com/a/22699894/1559840) solves the issue. – Amir Nissim Jun 25 '18 at 07:55

3 Answers3

16

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]
JoseKilo
  • 2,343
  • 1
  • 16
  • 28
8

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.

kdev
  • 141
  • 3
0

I have a coworker that had same issue. Maybe OP is typing

:Wq

Instead of

:wq

I would suggest exiting with

:x

To avoid this problem.

morhook
  • 685
  • 7
  • 19