Git: How to reuse/retain commit messages after 'git reset'?

GitCommitGit RebaseGit ResetCommit Message

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbentolorView Question on Stackoverflow
Solution 1 - GitibizamanView Answer on Stackoverflow
Solution 2 - Gituser2718704View Answer on Stackoverflow
Solution 3 - Gitmart1nView Answer on Stackoverflow
Solution 4 - GitJoeView Answer on Stackoverflow