Question

I use external editor to fill comments for "git commit", if by some reason commit fails all comments disappear. Is there some place where my comments for commit that's failed are stored ?

Is there any specific git command to reuse such pending comments or repeat commit with pending comments ?

Was it helpful?

Solution

tl;dr:

  1. Run git commit -eF .git/COMMIT_EDITMSG

or

  1. Run (only once): git config --global alias.commit-reuse 'commit -eF .git/COMMIT_EDITMSG'
  2. Run (whenever you want to reuse a message): git commit-reuse

If you write your commit message in an external editor, the message is stored in .git/COMMIT_EDITMSG. Even if the commit subsequently fails, the message will be stored there. The next time you do git commit, however, git resets the file and opens the editor there.

You can use the -F option to tell git to use the contents of an existing file as your commit message. After your failed commit, you can do git commit -F .git/COMMIT_EDITMSG, and git will immediately create the commit with the message that is stored in that file. However, note that commented lines will be included in the commit message if you do this:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i nor -o; assuming --only paths...
# ...

You can additionally use the -e option to ask git to open the file in an editor. Commit messages made with the editor will never include commented lines. This will also allow you to edit the message before creating the commit. So, simply use git commit -eF .git/COMMIT_EDITMSG after a failed git commit. Note, however, that this command fails if .git/COMMIT_EDITMSG does not exist, for example if you just cloned the repo.

If you don't want to remember the -eF .git/COMMIT_EDITMSG option, you can create an alias within git. You can permanently add an alias by running

git config --global alias.commit-reuse 'commit -eF .git/COMMIT_EDITMSG'

From now on, you can write git commit-reuse whenever you want to reuse the message of a failed commit. If you want to remove the alias again, you can remove the relevant line from ~/.gitconfig.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top