49

After I merge a feature branch back to main branch I usually need to do a merge commit by default. But I'd like to use the original commit messages from my feature branch in this commit instead of "merge branch XXX".

How should I do that?

kakyo
  • 10,460
  • 14
  • 76
  • 140

5 Answers5

105

Just pass the -m and --no-ff parameters to the merge command:

$ git merge other-branch -m "Commit Message" --no-ff

The --no-ff parameter is required to prevent merging as a fast-forward without merge commit.

Oshawott
  • 539
  • 5
  • 12
Joe Zim
  • 1,787
  • 1
  • 15
  • 16
  • wont this make a new message as "commit message" instead of copying the feature branch commit message? – Ricky-U Nov 23 '21 at 15:56
  • It will do that merge as a separate commit (no fast-forward if it's possible) with that message as the commit message. – Joe Zim Nov 26 '21 at 04:42
  • 1
    It also needs a `--no-ff` command. Without it, there will be no merge commit message. So here it goes: `git merge --no-ff other-branch -m "Commit Message"` – TinaTT2 Apr 30 '23 at 12:33
17

You basically have two option.

Easy solution: don't merge, Rebase
Rebase your branch on top of the main branch, resolve conflict, and then merge. As you'll have a straight history, you'll be able to fast-forward to merge and this won't create any merge commit.

git checkout feature
git rebase main
# Resolve conflict if there is
git checkout main
git merge feature

Second option: Rebase -i
You can edit your history after your merge (before you push to a remote). You can manage this with rebase interactive mode.

git checkout main
git merge feature
#You merge and resolve conflict
git rebase -i <sha of the commit before the merge>

Then you'll be taken into an interactive shell with the list of the commits, e.g.:

pick 73c991e Create progress bar module
pick b8a0b83 merge branch feature
pick 2120f47 Add user form
pick 70c55e4 Quiz prototype system

You just need to add squash or s instead of pick:

pick 73c991e Create progress bar module
pick b8a0b83 merge branch feature
s 2120f47 Add user form
pick 70c55e4 Quiz prototype system

This command would squash together b8a0b83 and 2120f47. The next step will be a commit text editor where you have both commit message combined, and it's now up to you to edit them correctly to only keep your original message.

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • 1
    Thanks. I use squash a lot in a single branch to combine commits. But I didn't know that merged commits can be squashed too. Thanks for the tip. – kakyo Feb 22 '13 at 18:15
  • On 2nd thought, I'm curious why I have to choose rebase over merge. I'm quite happy with what merge does except for the fact that it does not give the right message. I found that I can "git ci --amend" to change it. And according to this post: http://stackoverflow.com/questions/804115/git-rebase-vs-git-merge/ , rebase changes the history where I branched out the feature branch. I'd like to keep that history. – kakyo Feb 22 '13 at 18:28
  • 1
    Then you can also simply edit a commit message with `git rebase -i`. Instead of using `squash`, only use `edit`. I'm not a big protector of keeping the history in Git, to me Git is an editor to write your project history how it make sense and how it's easy to use (e.g. bisect). As so, I use extensively rebase to keep my history tidy and useful; this may not be your opinion, but I'm sure it'll be eventually. – Simon Boudrias Feb 22 '13 at 18:57
7

If the commit has been already done, you would just amend the commit.

If before the commit, use a git merge like in the example below:

$ git checkout mainBranch
$ git merge featureBranch --squash --no-commit

You may have to resolve conflicts.

This method avoids the automatic commit, and all files are left on the index; thus, you can commit the code with any commit message you want.

Ricardo Nolde
  • 33,390
  • 4
  • 36
  • 40
5

I found that after doing the merge commit. I can add an amendment commit "git ci --amend" to change the commit message, which does exactly what I asked for. I'll accept my own answer as the correct answer.

Simon Boudrias and ranendra gave relevant answers that are also effective in a different way. So I voted them up.

kakyo
  • 10,460
  • 14
  • 76
  • 140
4

When you have different changesets in master and the local branch, git creates an extra commit for the merge automatically. To stop such extra commits from happening, you can rebase the master in the branch before merging it to the master.

$ git pull(in master which retrieves the new changes>
$ git checkout <local_branch>
$ git rebase master 
$ git checkout master
$ git merge local_branch.
ranendra
  • 2,492
  • 2
  • 19
  • 29