8

Suppose I am part of a development team and we all work on different branches from a same project:

     o--o--o--o (Bob)
    /
o--o---o---o---o---o--o (Me, master)
        \
         o--o--o---o (Alice)

Today, I discover a bug in a common file to Alice and Bob. This is an old bug.

How do I fix it in the other branches? What is the workflow in this situation?

I suppose the correct way to proceed is to generate a patch that I personally email to Alice and Bob: "Hi, Alice and Bob, I found a bug that appeared on commit 7a6b87f. Please find the patch attached to this email".

The solution seems to be very simple if there is only three people working on the project. How do I manage it on a bigger project with more branches and more developers?

Of course if I am alone on the project I can simply use this solution.

Community
  • 1
  • 1
nowox
  • 25,978
  • 39
  • 143
  • 293

4 Answers4

9

You could do what David, states, but instead of merging, you can use cherry-pick command. Another option using cherry-pick is to just cherry-pick the changes that you had made on your branch to the other branches. Before doing that this way you have to be sure that the changes on the commit belong to and only to the bug fix. Then you simply go to each of the other branches and git cherry-pick <commit hash> if there any conflicts you will have to resolve it. That's the right way to do it, I think, I'm doing that way and seems more git-way.

Here the docs: http://git-scm.com/docs/git-cherry-pick

xiumeteo
  • 941
  • 7
  • 16
5

I would create a branch off of the common ancestor of Alice and Bob, fix the bug there, and then merge that branch into Alice, Bob, and master. This way you only fix the bug once, in a single commit.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • Well, I still need to checkout all branches and do the merge. Moreover I will need to inform all the developers about the bug. – nowox Jun 24 '15 at 15:41
  • There is now way to not inform your developers. I assume each developer has cloned from one master repository (origin). You can see every clone of each developer as a remote branch, even if the master repository doesn't know about that branch. As long as you don't have write access to all developer machines and know where they checked out working clones are, you need your developers to pull in the bug fixing change. – ikrabbe Jun 24 '15 at 15:44
3

you can easly bash script your way out of this with

for branch in branch1 branch2 branch3; do git checkout $branch && git cherry-pick a970d6ecd; done;

I like the common ancestor solution too then :

 for branch in branch1 branch2 branch3; do git checkout $branch && git merge common-ancestor-branch; done;
JuanitoMint
  • 501
  • 3
  • 11
2

The mail solution was used before git implemented git and ssh protocol exchange methods and can be used to send people patches for repositories that are not connected to the internet. The simplest way to apply patches from a mail is to write them into an mbox file and use git am. But as most people don't even know what an mbox file is, that method isn't used quite often.

Check git help merge-base to find a point where to branch off for a fixing commit.

If you really want to fix in a single commit you can just write that commit anywhere and cherry-pick it for all the branches if you have write access to those branches.

In bigger teams it is quite serious for every developer to define a common base, to merge with regularly.

ikrabbe
  • 1,909
  • 12
  • 25
  • 1
    Upvoting, "As most people don't even know what an mbox file is, that method isn't used quite often." - good justification, and made me laugh ;-) – Jan Molak Feb 09 '17 at 17:35