1

After merging a branch with a conflict git autogenerates the commit message which looks like:

Merge branch {branchname} into {branchname}

Remote repo which I am currently working with got commit message verification and only passes messages of a specific template. If I push a branch with the commit message like given before, it gets refused because it does not match the template. Intellij Idea allows to reword only commits written by me and calling the git rebase -i HEAD~3 is not showing that commit in the opened editor.

If there is a way to rename/change that commit message? Thank you.

nbelousov
  • 21
  • 3

3 Answers3

2

Try git rebase -p -i HEAD~3.

The man page says:

  -p, --preserve-merges
       Recreate merge commits instead of flattening the history by
       replaying commits a merge commit introduces. Merge conflict
       resolutions or manual amendments to merge commits are not
       preserved.

       This uses the --interactive machinery internally, but combining it
       with the --interactive option explicitly is generally not a good
       idea unless you know what you are doing (see BUGS below).

You should be okay though. The BUGS section reads:

 ... Editing commits and rewording their commit messages should work fine, 
but attempts to reorder commits tend to produce counterintuitive results.  

For example (and git hist below is an alias to git log with a few options to display nicely):

atsaloli@Aleksey_X1_C2G:~/git/stackoverflow-61125266$ git hist
* ecbdcc5 2020-04-09 | Add pineapple (HEAD) [Aleksey Tsalolikhin]
*   f9353df 2020-04-09 | Merge branch 'mybranch' into HEAD [Aleksey Tsalolikhin]
|\
| * aadd990 2020-04-09 | add cherry (mybranch, master) [Aleksey Tsalolikhin]
|/
* 166d386 2020-04-09 | Add pear to fruit list [Aleksey Tsalolikhin]
* 6a64ac2 2020-04-09 | Add bnana to fruit list [Aleksey Tsalolikhin]
* f42522f 2020-04-09 | Initialize fruit list [Aleksey Tsalolikhin]
atsaloli@Aleksey_X1_C2G:~/git/stackoverflow-61125266$                              

And:

atsaloli@Aleksey_X1_C2G:~/git/stackoverflow-61125266$ git rebase -p -i HEAD~4                                                              
...                                                                                 
pick 6a64ac2 Add bnana to fruit list
pick 166d386 Add pear to fruit list
pick aadd990 add cherry
pick f9353df Merge branch 'mybranch' into HEAD
pick ecbdcc5 Add pineapple

Now I can edit the merge commit message.

BTW, looks like this question is a duplicate of Git: How to edit/reword a merge commit's message?

Aleksey Tsalolikhin
  • 1,518
  • 7
  • 14
1

Have you tried git commit --amend ?That should allow you to amend the message on your last commit.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Aleksey Tsalolikhin
  • 1,518
  • 7
  • 14
  • Sorry, must have included this in the message. Not tried --amend as this is not the last commit message, and as far as I know it changes only the last one. – nbelousov Apr 09 '20 at 16:20
  • 1
    Got it, thanks, @nbelousov. And yes, you are right about that, `git commit --amend` changes the last one. – Aleksey Tsalolikhin Apr 09 '20 at 16:55
1

By default git rebase -i does not include merge commits. It tries to intelligently create a linear history instead of reproducing merges.

To edit your commit message, you can run git commit --amend which will allow you to edit the message in the default text editor or git commit --amend -m "Write your message here" to write your message directly on the command line.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268