Search code examples
gitgit-rebasegit-cherry-pick

How to change the branching point in Git?


Suppose I have the following structure in my git repo:

          C'---D'---E'---F'    (dev)
        / 
A---B---C---D---E---F       (master)

I would like to pull C' into the master branch and change the branching point to C', i.e., I want to turn the above into

              D'---E'---F'     (dev)
             / 
A---B---C---C'---D---E---F  (master)

I believe I should use a combination of cherry-picking and rebasing, but I am confused about how to do so exactly. Which sequence of operations should I perform?


Suggestion after comments for a clearer schema (since same-letter commits in the original were in fact unnecessary)

          G---H---I---J   (dev)
         / 
A---B---C---D---E---F     (master)

with the associated expected result :

              H---I---J    (dev)
             / 
A---B---C---G---D---E---F  (master)

Solution

  • The short answer, for your new diagram, is you rebase off G with:

    git checkout master
    git rebase <commit_id_G>
    

    The longer answer is that you're having difficulty seeing this because you're thinking of master as the "main" branch, and thinking of dev as the "offshoot". But, of course, these branch names are just labels, and you can consider either of them as the "main" branch.

    And, since master is the branch we actually want to change, we can redraw the diagram like this:

              H---I---J        (master)
             / 
    A---B---C---D---E---F---G  (dev)
    

    with the associated expected result :

                  H'--I'--J'    (master)
                 / 
    A---B---C---D---E---F---G   (dev)
    

    Hopefully it's easier to see here, that we simply need to rebase master off this new D instead of C.