Rajat Srivastava Rajat Srivastava - 4 months ago 8
Git Question

How to detach a branch and reattach it to some different branch in Git

I have a master and topic branch forked from it as shown below:

A---B---C---D master
\
E---F topic


I want to detach this topic branch and attach it to my feature branch like shown below:

G---H---I---J feature
\
E---F topic


Here master and feature branches are present on both remote and local while the topic is only at my local. I want to push topic after reattaching it to feature.

Thanks

Answer

If feature-branch contains B, then it's as simple as git rebase feature-branch from the topic branch. If this is not the case, you'll need:

git rebase --onto feature-branch B

The difference is because if B is not contained in feature-branch, the rebase command won't know how far to wind back before replaying.

In general, git rebase --onto X Y means "replay all commits after Y on top of X". git rebase X is shorthand that first finds the common ancestor of HEAD and X (Z = git merge-base HEAD X), then replay all commits after Z on top of X.

Docs: https://git-scm.com/docs/git-rebase

Comments