4

I have develop git branch for my work, I created a new branch called feature1 and made commit 1,2,3, and 4.

I need to move commit 3,4 from feature1 branch to a new created branch feature2.

The commits 3,4 should be deleted from feature1 and added to a new branch feature2, so the end result should be something like feature1 with 1, and 2 branches and feature2 with 3 and 4.

Please note that at the moment I have develop and feature1 branches. feature2 not added yet.

What is the best way to achieve that? I tried git cherry-pick but wanna make sure the best way to do that.

2
  • Where do you intend to create branch2? On which commit? Commented Sep 24, 2014 at 18:31
  • I want to create branch2(feature2) from develop branch and add commit 3,4 from feature1. Commented Sep 24, 2014 at 18:32

1 Answer 1

6

If I understand your description correctly, your repo currently looks like this,

... -- o [develop]
        \ 
         1 -- 2 -- 3 -- 4 [feature1]

and you want it to look like that

         3'-- 4'[feature2]
        /
... -- o [develop]
        \
         1 -- 2 [feature1]

Correct? If so, do the following.

First, make sure you're in a clean working state. Then, create and check out a branch called feature2 that points at the same commit as develop:

git checkout -b feature2 develop

Your repo will look as follows.

... -- o [HEAD=feature2,develop]
        \ 
         1 -- 2 -- 3 -- 4 [feature1]

Cherry-pick the two commits of interest (3 and 4):

git cherry-pick <commit-ID-of-3> <commit-ID-of-4>

After that, Your repo will look as follows.

         3'-- 4'[HEAD=feature2]
        /
... -- o [develop]
        \ 
         1 -- 2 -- 3 -- 4 [feature1]

Finally, check out your feature1 branch and reset it two commits back:

git checkout feature1
git reset --hard HEAD~2

Your repo will end up as desired,

         3'-- 4'[feature2]
        /
... -- o [develop]
        \
         1 -- 2 [HEAD=feature1]

and you'll be in a clean working state.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.