tdao
tdao

Reputation: 17668

git move/migrate commits from one branch to another

I want to create a new branch and move several commits from MASTER to BRANCH. I already created BRANCH from MASTER commit A.

I can use cherry-pick, but looks like rebase would be better choice.

I tried a command like this (from BRANCH):

git rebase --onto BRANCH A Z   

but the output is not as expected:

    Note: checking out 'b98885d'.

    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.

    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:

    git checkout -b new_branch_name

Please point out where I got wrong - thanks.

enter image description here

Upvotes: 0

Views: 233

Answers (1)

Ry-
Ry-

Reputation: 224913

From master at Z, copy to BRANCH:

git branch BRANCH

As you’re still on master, reset back to A:

git reset [--hard] A

BRANCH is now where master was, and master is now at A.

Upvotes: 4

Related Questions