Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git merge, how can I merge a bugfix into multiple long-term branches?

Tags:

git

merge

Recently at work, we've switched from using SVN to using git. We have to maintain multiple versions of our software, and we had them setup as branches before. So after using git svn clone, we ended up with 4 branches: master, 5.0, 5.1, and 5.2.

Note: We use master as our development branch. 5.0, 5.1, and 5.2 all branch from master and are used as production branches.

A few days have gone by and quite a few changes have gone into master. A bug was just found in 5.2 and we want to fix it. We've tried the the solution from Merging one change to multiple branches in Git with partial luck. We can always merge back into master easily, but whenever we merge back into 5.2 we get a bunch of conflicts. Everything we've seen that shows this process (creating a new branch for bugfix and merging it back into development and production) shows that it should be as simple as:

(master)$ git checkout -b bugfix
# fixed bug and committed

(bugfix)$ git checkout master
(master)$ git merge bugfix
# successful merge

(master)$ git checkout 5.2
(5.2)$ git merge bugfix
# successful merge

However for us when we get to that final line git merge bugfix we get tons and tons of conflicts. Conflicts for files that we haven't even touched in the bugfix. What are we doing wrong?

Note: We have also tried doing this process by starting the bugfix branch from 5.2. Then merging the bugfix into 5.2 is easy but merging the bugfix into master generates the conflicts.

Other places that show the process:

  • http://nvie.com/posts/a-successful-git-branching-model/#hotfix-branches
  • https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow (scroll to "End-user discovers a bug")
like image 637
Aust Avatar asked Jan 30 '15 21:01

Aust


People also ask

Can you merge a branch into multiple branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

Can you merge a branch into master multiple times?

Merging a branch multiple times into another works fine if there were changes to merge. Show activity on this post. Actually yes you totally can, when you merge again it will bring over any commits that don't exist on production.

Does git merge change both branches?

No, merging does only affect one branch.


1 Answers

git merge is of all changes since a common base. If you want to record a merge of just one change to multiple histories, you have to make it the only change to some ancestral content in all the histories, like so:

git checkout -b bugfix `git merge-base 5.0 5.1 5.2 master`
# so now bugfix is an ancestor of all of the above

# fix the bug here, then:
git commit -m 'bug fixed'

git checkout 5.0; git merge bugfix     # bugfix has just the one change
git checkout 5.1; git merge bugfix
git checkout 5.2; git merge bugfix
git checkout master; git merge bugfix

It might be better to go all the way back and branch bugfix off the commit that introduced the bug in the first place, but that's not always going to make for a clean merge if it's been around long enough.

like image 190
jthill Avatar answered Oct 24 '22 06:10

jthill