开发者

How do I separate a commit into a branch after doing several other commits?

开发者 https://www.devze.com 2023-01-25 07:48 出处:网络
I have a Git repo wi开发者_JAVA百科th the following structure: A--B--C--D--E I’d like to separate the C commit into a branch:

I have a Git repo wi开发者_JAVA百科th 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?


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


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''
0

精彩评论

暂无评论...
验证码 换一张
取 消