Question

I have a Git repo with the following structure:

A--B--C--D--E

I’d like to separate the C commit into a branch:

     C
    / \
A--B---D--E

How do I do that?

Was it helpful?

Solution

git branch new-branch C

will create a new branch pointing to C named new-branch, ending up with this:

    new-branch     HEAD
          |         |
A -> B -> C -> D -> E

OTHER TIPS

The correct answer is to honour the fact that D has 2 parents in the output that you want. I'm going to assume E is being pointed to by master.

git branch new-branch C
git checkout -b merge-base B
git merge --no-ff new-branch
git rebase --onto merge-base D^ master
git checkout master

you will end up with this:

     C
    / \
A--B---Y--D'--E'

this will preserve C as a parent in a merge into the main branch. You could squash D into Y with git rebase -i head^^^. You would then have:

     C
    / \
A--B---D''--E''
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top