ChrisWard
ChrisWard

Reputation: 353

Amending the message of Git commit made before a merge

I committed some test code before merging in a remote branch. This merge had a lot of conflicts and took some serious time to put right. So my history looks something like this:

7ab562c Merge from remote branch
... whole load of commits brought across from the remote branch...
f3e71c2 Temporary TESTING COMMIT

The test code is fine, I really just want to change the commit message. Normally I'd go right ahead with a git rebase -i f3e71c2^ (since none of this has been pushed yet), but I've been told by a colleague that this will mess up the merge. I really don't want to mess up the merge :)

Is my colleague correct? And if so, is there anything I can do, or do I just need to live with this history?

Upvotes: 28

Views: 11204

Answers (3)

VonC
VonC

Reputation: 1323953

2013: You can try a git rebase --preserve-merges --interactive, with:

-p
--preserve-merges

Instead of ignoring merges, try to recreate them.

The BUG section of the man page includes:

The todo list presented by --preserve-merges --interactive does not represent the topology of the revision graph.
Editing commits and rewording their commit messages should work fine, but attempts to reorder commits tend to produce counterintuitive results.


2019+: As I mentioned here, this option is deprecated since Git 2.22, Q2 2019.

You would need to use git rebase --rebase-merges --interactive instead.


As jthill's comment describes (since -p will better preserve merges if conflict resolutions were recorder):

You can retroactively light rerere for a merge:

git config rerere.enabled true
git checkout $merge^1

git merge $merge^2
# for Windows: 
# git merge $merge^^2

git read-tree --reset -u $merge
git commit -m-
git checkout @{-1}

As noted by Soufiane Roui in the comments:

For Windows CMD users, use double caret to point for parents of the merge commit (e.g $merge^^1 instead of $merge^1).
Because the caret is considered as an escape character.

Upvotes: 36

sebazelonka
sebazelonka

Reputation: 990

Update on this. I've tried -p as suggested by @vonc but didn't work for me.

Git docs now mentions -r or --rebase-merges, so full command looks something like this:

git rebase -i -r '<commit hash>'

https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt---rebase-mergesrebase-cousinsno-rebase-cousins

Upvotes: 2

kampu
kampu

Reputation: 1421

If and only if your colleagues have not pushed/pulled the changes ontop of f3e71c2 elsewhere, this will work. Otherwise I don't know what will happen. Changing the commit message is entirely cosmetic (== metadata change), given that you haven't pushed the commit you want to amend yet, but this could still result in history confusion if your colleagues have pushed/ pulled any part of the history which is atop it.

(thanks to Abizern for pointing out this failure-mode)

Upvotes: -4

Related Questions