Home > database >  How to move a branch on GitHub?
How to move a branch on GitHub?

Time:10-15

Currently my branches look like this:

     Branch A
    /
main-Branch B
             \
              Branch C

But I want to look like this:

     Branch A
    /
main-Branch B
    \
     Branch C

How to move the branch C, so that it branches from main (on remote)?

CodePudding user response:

You can use git rebase --onto and then force push the branch to your remote. Note that force pushing can cause a lot of damage, so know what you are doing.

See this section from git help rebase:

Another example of --onto option is to rebase part of a branch. If we
have the following situation:

                                H---I---J topicB
                               /
                      E---F---G  topicA
                     /
        A---B---C---D  master

then the command

    git rebase --onto master topicA topicB

would result in:

                     H'--I'--J'  topicB
                    /
                    | E---F---G  topicA
                    |/
        A---B---C---D  master

This is useful when topicB does not depend on topicA.

In your case that would be, if you are on branchC:

$ git rebase --onto main branchB

CodePudding user response:

This is a classic rebase problem found in the git-rebase documentation.

You need to specify that you only want to rebase the commits from branchB to branchC onto main.

git rebase --onto main branchB branchC
  • Related