207

How do I edit or reword a merge commit's message?

git commit --amend works if it's the last commit made (HEAD), but what if it comes before HEAD?

git rebase -i HEAD~5 doesn't list the merge commits.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • Two notes apply here: (1) Whether you use `git rebase -i -p` or `git rebase -i -r`, what you're doing is *re-performing* the merge. If the original merge had merge conflicts, the re-performance will have them again. (2) As with all rebase operations, this makes *new* commits; the old commits remain, but are abandoned from this branch. – torek May 21 '21 at 00:04
  • What does the `~5` represent? – AlikElzin-kilaka Dec 23 '21 at 20:16
  • @AlikElzin-kilaka `HEAD~5` refers to the great-great-great-grandparent of the current commit. See `git help rev-parse`. – ma11hew28 Dec 24 '21 at 13:20

8 Answers8

268

If you add the --preserve-merges option (or its synonym, -p) to the git rebase -i command then git will try to preserve the merges when rebasing, rather than linearizing the history, and you should be able to amend the merge commits as well:

git rebase -i -p HEAD~5

Note. --perserve-merges has been deprecated in favour of --rebase-merges as of git v2.22 (https://www.infoq.com/news/2019/07/git-2-22-rebase-merges/).

prasanthv
  • 2,442
  • 2
  • 21
  • 17
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    I've done this but after making my changes and I try and push up my changes I get this `! [rejected] HEAD -> master (non-fast-forward)error: failed to push some refs to ` – Marc Mar 26 '13 at 14:58
  • 2
    try to run git push -f and then your origin branch. this should work. I had the same problem, for some reason this is an artifact of rebasing because what basically happened is that after rebasing you ended up with a detached hed, so the -force should fix that and should push everything. – Radu Comaneci Apr 05 '13 at 08:46
  • 13
    @Marc This happens because you modified commits that you already sent. It is considered bad practice to force push to a server since it can completely desync you and your co-workers. Well, if you're alone it should not be a problem. – ibizaman Jan 14 '14 at 08:56
  • Where `HEAD~5` is the parent of the commit you want to modify (usually sha1^). – Gabriel Devillers Jan 23 '19 at 17:08
  • 14
    `--preserve-merges` is now `--rebase-merges` – OrangeDog Jun 27 '19 at 09:40
  • `HEAD~` (rather than `HEAD~5`) is enough if the merge commit is the commit where you are (e.g. you just commited it and then spotted a typo). – Stéphane Gourichon Jan 04 '20 at 14:38
  • This just seems to create huge mess of a commit message for me (meaning rebase editor in vim). Not sure why. There are like hundreds of commit messages included when I call `git rebase -i --rebase-merges HEAD~3` where HEAD~3 is first parent of the merge commit I want to reword. Instaead using combination of: `git log --oneline 3`, `git reset --hard HEAD~2`, `git commit --amend` and `git cherry-pick ..` is much less of a hassle. I could also use origin branch name to write the cherry-pick: `git cherry-pick origin/branch_name~3..origin/branch_name`. – user232548 Oct 15 '20 at 09:08
  • @user232548 and @OrangeDog `--rebase-merges` is not the same thing as `--preserve-merges` at all! I had the same problem with `--rebase-merges`, but `--preserve-merges` worked fine. – Katharsas May 18 '21 at 15:10
39

Note that, starting git1.7.9.6 (and git1.7.10+), git merge itself will always trigger the editor, for you to add details to a merge.

"git merge $tag" to merge an annotated tag always opens the editor during an interactive edit session. v1.7.10 series introduced an environment variable GIT_MERGE_AUTOEDIT to help older scripts decline this behaviour, but the maintenance track should also support it.

It also introduces an environment variable GIT_MERGE_AUTOEDIT to help older scripts decline this behavior.

See "Anticipating Git 1.7.10":

Recently in a discussion on the Git mailing list, Linus admitted (and I agreed) that this was one of the design mistakes we made early in the history of Git.
And in 1.7.10 and later, the git merge command that is run in an interactive session (i.e. both its standard input and its standard output connected to a terminal) will open an editor before creating a commit to record the merge result, to give the user a chance to explain the merge, just like the git commit command the user runs after resolving a conflicted merge already does.

Linus said:

But I don't really care deeply how it actually works - my main issue is that git makes it way too easy to have bad merge messages.
I think part of that is an even simpler idiocy: we never even fire up the editor by default for a "git merge", but we do for a "git commit".
That was a design mistake, and it means that if you want to actually add a note to a merge, you have to do extra work. So people don't
.


Note that, before Git 2.17 (Q2 2018), "git rebase -p" mangled log messages of a merge commit, which is now fixed.

See commit ed5144d (08 Feb 2018) by Gregory Herrero (``).
Suggested-by: Vegard Nossum (vegard), and Quentin Casasnovas (casasnovas).
(Merged by Junio C Hamano -- gitster -- in commit 8b49408, 27 Feb 2018)

rebase -p: fix incorrect commit message when calling git merge.

Since commit dd6fb00 ("rebase -p: fix quoting when calling git merge", January 2018, Git 2.16.0-rc2), the commit message of the merge commit being rebased is passed to the merge command using a subshell executing 'git rev-parse --sq-quote'.

Double quotes are needed around this subshell so that, newlines are kept for the git merge command.

Before this patch, following merge message:

"Merge mybranch into mynewbranch

Awesome commit."

becomes:

"Merge mybranch into mynewbranch Awesome commit."

after a rebase -p.


With Git 2.23 (Q2 2019), A "merge -c" instruction during "git rebase --rebase-merges" should give the user a chance to edit the log message, even when there is otherwise no need to create a new merge and replace the existing one (i.e. fast-forward instead), but did not.
Which has been corrected.

See commit 6df8df0 (02 May 2019) by Phillip Wood (phillipwood).
(Merged by Junio C Hamano -- gitster -- in commit c510261, 13 Jun 2019)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
31

Another nice answer using only primitive commands -- by knittl https://stackoverflow.com/a/7599522/94687:

git checkout <sha of merge>
git commit --amend # edit message
git rebase HEAD previous_branch

or a better (more correct) final rebase command:

git rebase <sha of merge> previous_branch --onto HEAD

BTW, using the primitive commands might have the nice "feature" of not consuming too much CPU and making you wait unknown time until Git finishes thinking about the list of commits needing to be rebased in the case of git rebase -p -i HEAD^^^^ (such a command which would result in a list of only 4 last commits with the merge as last one in my case in my case took around 50 secs!).

Community
  • 1
  • 1
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
  • 3
    This is really useful, save me quite a bit of time. My company blocks some commit messages in the repository, which is easy with --amend or with rebase commands but: Big problem if we merge some branch into yours, do some commit and try to push, the default merge message of git is blocked (this should be fixed, I know) which force us to change that message. Until this answer I have tried many thing to change a merge message between a history of commits with not success. – Giovanni Silva Oct 26 '17 at 12:08
15

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).

Eugen Labun
  • 2,561
  • 1
  • 22
  • 18
  • Hi where to insert new commit message I have tried it many times but not changing can you help me a little into this – ThinkTank Dec 08 '20 at 09:09
  • @ThinkTank After replacing `merge -C` with `merge -c` (in the git-rebase-todo file) and starting rebase as usual (by saving the todo file), the rebase should stop on that merge commit and the editor should popup allowing you to change the commit message. Just like you would reword the normal commits messages by replacing `pick` with `reword`in the todo file. – Eugen Labun Dec 09 '20 at 10:47
  • 1
    I want to change merge message which is automatically added to the commit,done above steps but not changing!!! – ThinkTank Dec 09 '20 at 18:48
  • 1
    Same as my comment in a previous post, this solution helped me. – tarekahf Jan 25 '22 at 19:15
  • When `git rebase -i -r ` is run and the interactive editor opens, it seems the original merge commit message is appended to the line starting with `merge -C`, separated by a `#`. If that original message contains whitespace, the `git rebase ...` command fails. Workaround: Remove the contents of that original message / "comment", starting with the first word mentioned in the error message. – ssc Mar 26 '23 at 11:47
7

Update from 2021, -p is deprecated.

Use --rebase-merges instead.

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
6

Use the --rebase-merges (or the shortened -r) flag:

git rebase -i -r HEAD~5

Then change the 'pick' text to 'edit' or 'reword' next to the commit to change:

pick <commit-hash-to-leave> <message>
edit <commit-hash-to-change> <message>

The --rebase-merges (or the shortened -r) flag replaces the deprecated
--preserve-merges (or the shortened -p)

Documentation: https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--r

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
A Jar of Clay
  • 5,622
  • 6
  • 25
  • 39
4

git merge --edit
Allows you to give the comment even in case of non-interactive merge.

git merge --edit --no-ff can be useful if you follow git flow with rebasing on development branch and merging into it with no fast forward.

Do-do-new
  • 794
  • 8
  • 15
0

The git rebase -i HEAD~5 command pops up the editor. It lists the specified commits (in this case five of them). The first column contains pick for every commit. Just replace pick with reword in that editor and save+close the editor. Then git will pop up the editor for every commit where you changed pick to reword and will let you edit the commit message.

ChrisD
  • 105
  • 1
  • 1