user746461
user746461

Reputation:

Selectively split a branch into two

I have a branch which contains a number of commits, namely c1, c2, c3, c4.

master --> c1 --> c2 --> c3 --> c4

I want to pick c1 and c3 to a new branch, while c2 and c4 stay in current branch.

The final log shall look like

master --> c1 --> c3
        \
         -> c2 --> c4 

I know I can rebase a couple of times to achieve this, but is there a faster command to move a commit, or something like interactive rebase and put selected commits to another branch?

Upvotes: 1

Views: 345

Answers (1)

ElpieKay
ElpieKay

Reputation: 30868

The master in the graph is not clear. I just suppose it's the root commit c0 of the branch.

1.cherry-pick

git checkout -b new1 c0
git cherry-pick c1 c3
git checkout -b new2 c0
git cherry-pick c2 c4

2.rebase

git rebase -i c0 c3
#======
pick c3
drop c2
pick c1
#======
git branch new1

git rebase -i c0 c4
#======
pick c4
drop c3
pick c2
drop c1
#======
git branch new2

new1 is c0-c1'-c3' and new2 is c0-c2'-c4'

Upvotes: 2

Related Questions