Git: How to reuse/retain commit messages after 

2019-01-30 22:22发布

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.

4条回答
三岁会撩人
2楼-- · 2019-01-30 22:25

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-30 22:41

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楼-- · 2019-01-30 22:45

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-30 22:50

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
查看更多
登录 后发表回答