Search code examples
gitbranching-and-mergingcherry-pick

Git workflow to add the same functionality to diverged branches


[There seems to be a lot of similar information about related topics, but I still cannot find the answer.]

Suppose we have two diverging branches: stable and master (aiming at becoming the next stable):

----> split --> fix 1 -->  [stable]
         \
           ---> development --> more development ----> [master]

The branches diverged, for example, because fix 1 was done differently in master, as some development presented a better way to do it (but the development itself was too large in scope to merge it into the stable branch).

Now we have another bug 2, for which we need fix 2. While the branches are already diverged, the affected files are absolutely the same in both stable in master and the fix would look exactly the same too.

What is the best way to apply identical changes to two diverged branches? Fix in one, then cherry-pick? Or is there a better solution?


Solution

  • The problem with cherry-pick is that you don't have an explicit link between both commits, defeating the purpose of keeping track of changes. Just do a merge:

    1. Find an appropriate common ancestor X of master and stable
    2. Create and switch to a branch named fix_bug2 at commit X
    3. Fix bug, commit
    4. Merge fix_bug2 into stable
    5. Merge fix_bug2 into master