How to import bugfix of master in all other branches without including the commits before?

96 Views Asked by At

I have some branches: branch 1 and branch 2. They have their origin from the master branch. Now there were some changes in the master branch. One is a critical bugfix, which has to be imported on branch 1 and 2. But there is a change (or perhaps multiple) which should not be imported on the other branches.

How is that solved in the GIT world?

Bugfix in master branch should be imported on other branches

One idea I came up with is to create the bugfix branch before the other changes are made to the master branch. This would work if you go back in history and you know that at that point the branches 1 and 2 are using the same code base as in the master branch.

What if branch 1 and 2 contain different versions of the master, but the affected code is the same?

branch 1: master v1.0
branch 2: master v1.1

I can't merge the latest version of the master into the other branches, because there are changes, which the branches shouldn't get yet. Should the changes be made in every branch?

3

There are 3 best solutions below

0
On BEST ANSWER

Merge instead of cherry-pick!

cherry-pick makes a new commit on your branch, and that commit doesn't not refer to where the bugfix was originally created, hiding actual history. Furthermore, the bugfix could be several commits, or have further development in the future (like, it didn't turn out to really fix it the first time). This makes cherry-pick inconvenient.

I'd much rather merge the bugfix branch. When you created the bugfix commit on master, surely you had a branch there?

$ git checkout master
$ git checkout -b bugfix_1234
... work work work
$ git commit -a -m"Fix the bug blablabla"
... oh snap, I forgot to clean blublu. Work work work
$ git commit -a -m"Clean blublu"
... test, it's fine, merge to master
$ git checkout master
$ git merge bugfix_1234 --no-ff
... mmm, my branches 1 and 2 also need the change
$ git checkout branch_1
$ git merge bugfix_1234
$ git checkout branch_2
$ git merge bugfix_1234

Done! The nice thing with this is that you don't have duplicate commits doing the same thing. Your history reflects also what you actually did, work on a branch, integrate its changes to other branches.

If you don't have the branch bugfix_1234 anymore, you can always recreate one, or an annotated tag.

Look at the red dot in this git workflow, it's exactly what it is doing.

Edit: as per your comment in another answer, you need your bugfix (hotfix in the workflow) to be based on a common ancestor of the branches and master. If the fix applies to both the branches and master, it makes sense anyway.

1
On

You can do like this .

Go in the branch you want apply the commit to.

git checkout branch-name

Execute the following git command:

git cherry-pick <commit-hash>
2
On

Your idea to branch off of master for the bug fix is a good one. After the fix, you will want to merge, not cherry-pick, bug into the different branches:

git checkout branch1
git merge bug
git checkout branch2
git merge bug
git checkout master
git merge bug

This way, when you merge master into one of the other branches later, git will know what has already been merged in. If you did a cherry-pick instead, git would not know where those changes came from, and you might get conflicts down the road.

By the way, you may want to look into Git flow; it addresses these kinds of issues nicely.