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 ?
To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.
To make it work for multiple commits, just create a temporary commit with your newest changes and then use an interactive rebase to squash the previous commit (containing the good commit message) with the new temporary one, keeping the commit message of the old commit.
That's easy to fix: You can amend the commit. Before you make any other change to the repository, simply run git commit --amend. Your default text editor will open and you'll be able to fix the commit message: Fix the message, save and close your editor, and Git will finish the operation.
tl;dr:
git commit -eF .git/COMMIT_EDITMSG
or
git config --global alias.commit-reuse 'commit -eF .git/COMMIT_EDITMSG'
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With