Search code examples
gitrebase

git - Move existing branch to new child branch


I have the following situation:

                  F---G---H BranchB
                 /
A---B---C---D---E---I---J BranchA

and I want that BranchA point to BranchB (commit H) and discard commits I and J, removing then BranchB:

A---B---C---D---E---F---G---H BranchA

Any idea about how to achieve that?


Solution

  • To move a branch to a different commit you can use the git reset command.

    Thus, the steps to make BranchA refer to the same commit as BranchB does you can use this sequence of git commands:

    git checkout BranchA
    git reset --hard BranchB
    

    This will both move the branch pointer as well as clean up your working folder with the files from BranchB (which is now also the files of BranchA).

    Note that if you have already pushed BranchA to a remote, you might be denied pushing the updated branch unless you use "force-push".

    When using force push, ensure you do not lose commits in the remote, but here's how to force-push your updated BranchA branch:

    git push -f