1

Say I have two branches in Git: development and qa-test. Both of these branches contain different features - commits but also A is the latest common ancestor of these two branches:

A--->B--->C--->developent
\
 \
  D--->E--->qa-test

Now in some point we have found out that there was a bug in A, and we need to have fixed them both in qa-test and in development, i.e. we need to hotfix. One of the ways I can go is to create a new branch from qa-test names hotfix, fix there the bug, commit in into qa-test and then cherry-pick this into development. Is there a better way of doing this?

Community
  • 1
  • 1
Narek
  • 38,779
  • 79
  • 233
  • 389

3 Answers3

2

Create a branch from A and then merge the changes of the with qa-test and development.

$ git merge-base qa-test development 
sha-1 A
$ git checkout -b hotfix <<sha-1 A>>
... do changes and commits
$ git checkout devel
$ git merge hotfix
$ git checkout qa-test
$ git merge hotfix
$ git branch -d hotfix 
Pigueiras
  • 18,778
  • 10
  • 64
  • 87
  • This is a good way but what if `A` is not a branch but just a commit? And how to find the common ancestor? And what about conflicts? – Narek Sep 19 '13 at 13:44
  • @Narek You can find the common ancestor with: http://stackoverflow.com/questions/7167645/how-do-i-create-a-new-git-branch-from-an-old-commit. And you can create a branch from any commit you want, I don't understand your first question. – Pigueiras Sep 19 '13 at 13:48
  • So we can do this: `git merge-base qa-test development`, before all your commands. – Narek Sep 19 '13 at 13:55
  • @Narek Yes, you can do that. For more info about creating branch from old commits you have this: http://stackoverflow.com/questions/7167645/how-do-i-create-a-new-git-branch-from-an-old-commit – Pigueiras Sep 19 '13 at 13:59
  • Yes, you are writing the same link twice :) Anyway thank you for the idea. I guess this is much better than cherry pick version. – Narek Sep 19 '13 at 14:07
  • @Narek Ouch, sorry, the first link was this: http://stackoverflow.com/questions/1549146/find-common-ancestor-of-two-branches. It was a miss-paste haha – Pigueiras Sep 19 '13 at 14:10
  • what if we introduce a new bug in `hotfix` branch that is already merged and we need to hotfix the `hotfix`. I mean in this case `A` already does not contain the hotfix changes where we have introduced the second bug. – Narek Sep 19 '13 at 14:29
  • @Narek You should create a new branch from the last commit of the hotfix. If you don't delete the branch is easy to know which is. – Pigueiras Sep 19 '13 at 14:52
  • Yes I have came to the same conclusion that I have to have for each hotfix one branch :( Cherry pick is simpler? – Narek Sep 19 '13 at 15:03
  • 1
    I only use cherry pick when I want to pick changes from a branch that I want to discard, because cherry pick generate duplicated commits in the history: http://davitenio.wordpress.com/2008/09/27/git-merge-after-git-cherry-pick-avoiding-duplicate-commits/. For example, if your branches are public you can't rebase for avoiding that. – Pigueiras Sep 19 '13 at 15:20
1

Expanding on @Konstantin's answer:

$ git checkout A
... fix bugs ...
$ git checkout -b fix # creates a temporary branch for the fix
$ git commit

The state now:

  fix
 /
A -> B -> C -> development
 \
  D -> E -> qa-test

Now for the rebase:

$ git checkout development
$ git rebase fix # uproot development and stick it on the fix branch
... fix any merge conflicts ...
$ git checkout qa-test
$ git rebase fix # same for qa-test
... fix any merge conflicts ...

The state now:

A -> fix -> B -> C -> development
       \
        D -> E -> qa-test

If I'm doing something horribly wrong here, please let me know so I can fix this answer!

SilverWolf
  • 284
  • 2
  • 13
0

"rebase" will do that for you, fix bug in development branch, then rebase qa-test with development

Konstantin
  • 3,254
  • 15
  • 20
  • Nope I think. I don't want `B` and `C` to be in `qa-test` as well as I don't want to have `D` and `E` in `development`. If I will `rebase` `qa-test` with `development` then I will have1 `hotfix`, `B` and `C` in `qa-test`. – Narek Sep 19 '13 at 13:42