Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amending the message of Git commit made before a merge

Tags:

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?

like image 893
ChrisWard Avatar asked May 14 '13 09:05

ChrisWard


People also ask

Can you edit git commit message?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.

Can we change commit message after merge?

For current Git versions (2020+), just do git rebase -i -r <parent> , then replace in the editor merge -C with merge -c . This will open the merge commit's message in the editor during rebasing, where you can change it (thanks to VonC for the hint).

How do I amend a prior commit?

You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.


1 Answers

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.


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.

like image 70
VonC Avatar answered Oct 13 '22 23:10

VonC