如何在Git中的分支之间移动提交?

时间:2022-06-05 07:26:06

I'm sure this is a simple thing that has been asked and answered, but I don't know what terms to search for. I have this:

我确信这是一个简单的问题并得到了回答,但我不知道要搜索的条款。我有这个:

    /--master--X--Y
A--B
    \--C--D--E

Where I commited C, D, and E (locally only) on a branch, but then I realized that D and E are really independent of C. I want to move C to its own branch, and keep D and E for later. That is, I want this:

我在一个分支上提交C,D和E(仅限本地),但后来我意识到D和E实际上是独立于C.我想将C移动到它自己的分支,并保留D和E以供日后使用。也就是说,我想要这个:

                   /--C
    /--master--X--Y
A--B
    \--D--E

How do I yank C out from under D and E?

我如何从D和E下面抽出C?

1 个解决方案

#1


43  

You can use git cherry-pick to grab C, and put it on Y. Assuming Y exists as the tip a branch called branch-Y:

您可以使用git cherry-pick来抓取C,并将其放在Y上。假设Y作为尖端存在一个名为branch-Y的分支:

$ git checkout branch-Y
$ git cherry-pick C

So now C is on top of Y. But D and E also still contain C (cherry picking doesn't move a commit, it just makes a copy of it). You'll have to rebase D and E on top of B. Assuming E is the tip of branch-E and B is branch-B, you can:

所以现在C在Y之上。但是D和E也仍然包含C(樱桃挑选不会移动提交,它只是复制它)。你必须在B之上重新定义D和E.假设E是分支-E的尖端而B是分支-B,你可以:

$ git checkout branch-E
$ git rebase --interactive branch-B

This will open up an interactive rebase session. Just remove commit C entirely, and leave D and E intact. You'll then have D and E rebased on top of B without C.

这将打开一个交互式rebase会话。只需完全删除提交C,并保持D和E不变。然后,您将在没有C的情况下将D和E重新置于B之上。

#1


43  

You can use git cherry-pick to grab C, and put it on Y. Assuming Y exists as the tip a branch called branch-Y:

您可以使用git cherry-pick来抓取C,并将其放在Y上。假设Y作为尖端存在一个名为branch-Y的分支:

$ git checkout branch-Y
$ git cherry-pick C

So now C is on top of Y. But D and E also still contain C (cherry picking doesn't move a commit, it just makes a copy of it). You'll have to rebase D and E on top of B. Assuming E is the tip of branch-E and B is branch-B, you can:

所以现在C在Y之上。但是D和E也仍然包含C(樱桃挑选不会移动提交,它只是复制它)。你必须在B之上重新定义D和E.假设E是分支-E的尖端而B是分支-B,你可以:

$ git checkout branch-E
$ git rebase --interactive branch-B

This will open up an interactive rebase session. Just remove commit C entirely, and leave D and E intact. You'll then have D and E rebased on top of B without C.

这将打开一个交互式rebase会话。只需完全删除提交C,并保持D和E不变。然后,您将在没有C的情况下将D和E重新置于B之上。