Git: How to reuse/retain commit messages after 

2019-01-30 21:59发布

问题:

As Git user I regular come across the situation, that I need to rework one or more commits in a way which do not fit into --amend or rebase -iwith fixup commits. Typically I would do something like

git reset HEAD~1
# hack, fix, hack
git commit -a
# argh .. do I need to retype my message?

I take sensible composed commit messages quite serious. They typically contain larger text with references & justifications for the change. Until now, I'm quite annoyed on the lengthy process to recover my old commit message via an unsorted git reflog, git logand copy & paste process.

Is there a better to tackle this? And how would it, if my comprises more than one commit?

Edit: After a bit thinking about this I think what I'm looking for is some git stash-like functionality for commit messages where fixup/amend commits are not appropriate.

回答1:

After a git reset, this one-liner can do it:

git commit --reuse-message=HEAD@{1}

or even shorter:

git commit -C HEAD@{1}

You can use the other options given by @user2718704.



回答2:

When running "git commit" command, you've to check the following options,

To reuse,

--reuse-message=<commit>

To edit on reuse,

--reedit-message=<commit>

To change the author,

--reset-author


回答3:

Why reset if you can hack, fix, hack and then just run git commit --amend --no-edit; thus, retaining your original commit message.

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.



回答4:

You could consider git commit --reset-author -c <commit>, to reuse the commit message with editing and the current time.