Git: How to reuse/retain commit messages after 'git reset'?
Solution 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.
Solution 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
Solution 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.
Solution 4
You could consider git commit --reset-author -c <commit>
, to reuse the commit message with editing and the current time.
bentolor
Updated on September 19, 2020Comments
-
bentolor almost 4 years
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
orrebase -i
with fixup commits. Typically I would do something likegit 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 log
and 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.
-
qqx about 11 yearsWhen doing an interactive rebase, you can even use the
fixup
instruction to declare that the later commit is to fix the previous commit and it will automatically use the commit message from the original discarding the message from the fixup commit. -
bentolor about 11 yearsFor example if I want to re-merge an force-updated pull-requests. Or if the commit is not the last one and cannot be easily fixed based on HEAD and its easier to redo them.
-
mart1n about 11 years@BenTebulin Well, interactive rebase lets you modify any commit in a range of specified commits. It's not strictly the HEAD commit that has to be modified.
-
bentolor about 11 years@mart1n Thanks for singling out edit in
rebase -i
. Never used it in that context. For the remaining cases like re-merging the other answer is more appropriate to my question, so I marked that one as answer. -
bentolor almost 11 yearsMarked this as the new solution, as this provided the most comprehensive answer. Though this solution still does not fully solving my 'retrieval' issues.
-
Phu Ngo almost 6 yearsShorter:
git commit -C@@{1}
-
David Mann almost 6 yearsIntellij has scopes that only operate on files that haven't been committed yet. Its useful to reset the files to uncommitted so that intellij can be instructed to lint the files, for example, and then re-commit those files with the same message. amend wont work for that due to limitations of intellij's scoping.
-
Scott Jacobsen about 5 yearsAfter a reset, ORIG_HEAD is set. I find
git commit --reuse-message=ORIG_HEAD
to be the most clear.