Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse pending comments after git commit fail?

Tags:

git

git-commit

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 ?

like image 235
sim Avatar asked Dec 14 '13 06:12

sim


People also ask

How do I change old commit messages?

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.

How do you reuse a commit?

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.

How do I save an amended 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.


1 Answers

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.

like image 172
janos Avatar answered Nov 03 '22 13:11

janos