Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merging hotfix to multiple branches

I've been trying to wrap my head around git branching models. I've been looking at http://nvie.com/posts/a-successful-git-branching-model/ for some ideas and coming from Subversion one thing I was really looking forward to was making a change in a single place and merging it to all the branches that needed it. In Subversion, we ended up doing to much copy of code around.

However I still don't get this completely. Here is a standard type of workflow that I have and it will always come up with conflicts.

# create new version branch git checkout master git checkout -b v3 vim pom.xml  # change branch version to "3.1-SNAPSHOT" git commit -a git checkout master vim pom.xml  # change master version to "4.0-SNAPSHOT" git commit -a 

So the master is at 4.0-SNAPSHOT and the branch is at 3.1-SNAPSHOT.

Not I want to create a hotfix on the branch and move it to the trunk.

git checkout v3 git checkout -b hotfix vim file.txt  # make a bugfix change git commit -a git checkout v3 git merge hotfix  # this works fine git checkout master git merge hotfix  # this has a conflict since both branches have changed the version 

I understand why its happening and it makes sense. Is there a better way of doing this?

I read about cherry-pick, which I tested and does work:

git checkout v3 git cherry-pick a518b0b75eaf28868 git checkout master git cherry-pick a518b0b75eaf28868 

However, that doesn't seem like the "correct" way to handle this. Any suggestions?

like image 543
Chuck M Avatar asked May 25 '12 21:05

Chuck M


People also ask

Can you merge multiple branches in git?

Merging Branches in a Local RepositoryTo 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.

Can I merge two feature branches?

Merge branchesMerging your branch into master is the most common way to do this. Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

How do I merge codes from one branch to another in GitHub?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.


2 Answers

If you wanted to get super-technical about it, you could create the hotfix from a common ancestor:

git merge-base v3 master git checkout -b hotfix <whatever you got from merge-base> # make your fix git checkout v3 && git merge --no-ff hotfix git checkout master && git merge --no-ff hotfix          v3--------v3 (hotfixed)        /         / ancestor----hotfix        \         \         master----master (hotfixed) 

The --no-ff flag is there to highlight that Git will create a merge commit, keeping the hotfix branch label at the hotfix tip, instead of pulling the label to v3 or master. (You can omit the flag and get the same behavior, since the hotfix branch has one commit that isn't in master or v3. More info in the docs.)

Personally, I think that's overkill. I'd go with gahooa: make the hotfix on the branch that makes sense, then merge or cherry-pick depending on how you want the branches to relate to each other.

like image 72
ellotheth Avatar answered Oct 24 '22 00:10

ellotheth


Really, your answer is dependant on if you want your trees to be based on the same history... For example, 4.0 is based on the latest 3.X + all of the changes in 4.0...

Personally, I don't recommend it once you decide to start a new branch(s) for a new version(s). At a give point of time, the software is taking a different direction, so your branches should also.

This leaves git cherry-pick as your ideal solution. Make the change in whatever branch makes the most sense, and then cherry pick it to the older versions. This is the same as if you had checked out the old branch and manually applied the same change, and made a new commit. It keeps it clean and to the point.

Git merge or rebase are going to try to integrate the branches history together, each in their own way, which I suspect you don't want when backporting bug fixes, etc...

like image 43
gahooa Avatar answered Oct 23 '22 23:10

gahooa