42

I am used to having one main branch (master) and working in topic branches. But I'm working on a project now with two main branches (master and experimental) and I am unsure how to best merge my topic branch into both?

Is this the right way to do it? If not can someone let me know the right way.

(master)$ git checkout -b bugfix
# do bug fix here
(bugfix)$ git commit -a -m 'Fixed bug.'
(bugfix)$ git checkout master
(master)$ git merge bugfix

(master)$ git checkout bugfix
(bugfix)$ git rebase experimental
(bugfix)$ git checkout experimental
(experimental)$ git merge bugfix

Thank you.

user1015384
  • 579
  • 1
  • 5
  • 7

1 Answers1

56

Don't do the rebase and you're set. Simply merge your bugfix branch into each branch you need it

(master)$ git checkout -b bugfix
# do bug fix here
(bugfix)$ git commit -a -m 'Fixed bug.'
(bugfix)$ git checkout master
(master)$ git merge bugfix

(bugfix)$ git checkout experimental
(experimental)$ git merge bugfix

When doing the rebase you are creating a commit similar to the already merged commit, but different. Doing the rebase followed by checkout+merge is essentially equivalent to cherry-picking the bug fixing commit.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thank you. Doing it this way, will only the merge commit show in master and experimental? Can I do it so my topic branch commits show? What is the usual thing to do here with Git? – user1015384 Oct 26 '11 at 20:47
  • After merging `bugfix` both master and experimental will contain all commits which are reachable from `bugfix` (do `git log bugfix` to show them). – knittl Oct 26 '11 at 20:49
  • ok thank you and finally when there is only 1 commit in the topic branch, can i avoid the separate merge commit appearing in master and experimental? – user1015384 Oct 26 '11 at 20:52
  • No, not if you merge it. Alternatively, you could fix the bug in master (or in a topic branch and then merge to master) and merge master into experimental, but this will still give you two merge commits. – knittl Oct 26 '11 at 20:55
  • 8
    Not sure if this works all the time. What if your bugfix branch is quite different from experimental. The you will have conflicts, and the merge will show more files than what you are actually trying to merge. Without re-syncing the source branch with the target branch, how can you make this work all the time? – endless Sep 17 '16 at 20:49
  • @endless: it really doesn't matter which direction you perform the merge. You will get the same conflicts (well, actually the reverse conflicts) if you try to merge the source into the target branch. – knittl Sep 18 '16 at 12:40
  • suppose master and experimental are totally different except for the file changed in bugfix. as bugfix is checkout from master, so it could be safely merged to master. but how about experimental? – Kingname May 08 '20 at 10:35
  • @Kingname do experimental and master have a common ancester? If so, create the bugfix branch from that ancestor commit (i.e. the merge base of both branches). if they don't, cherry-pick or rebase the fix to the other branch. – knittl May 08 '20 at 11:14
  • yes, they have common ancestor, one year ago. and after that, each branch has more than one thousand comment for themself. so except for some utility files, other files are totally different. now, I modify one utility function in a common file. – Kingname May 08 '20 at 11:48
  • 1
    @Kingname In that case, with such highly diverged branches, I would suggest to simply do the fix twice: one commit on each branch. Of course, you can use git cherry-pick to textually copy the patch of the commit to the other branch. – knittl May 11 '20 at 18:03