如何在Git中的分支之间移动提交? [英] How do I move a commit between branches in Git?

查看:128
本文介绍了如何在Git中的分支之间移动提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是一个简单的问题,已经被问到并回答了,但是我不知道要搜索什么术语.我有这个:

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

我在分支上提交了C,D和E(仅在本地),但是后来我意识到D和E确实独立于C.我想将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
    /--master--X--Y
A--B
    \--D--E

如何从C和D中抽出C?

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

推荐答案

您可以使用git cherry-pick来抓取C,并将其放在Y上.假定Y作为尖端存在,则称为branch-Y的分支:

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 checkout branch-Y
$ git cherry-pick C

因此,现在C在Y之上.但是D和E仍然包含C(樱桃采摘不会移动提交,它只会复制提交).您必须在B的顶部重新设置D和E.假设E是branch-E的尖端,而B是branch-B,则可以:

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:

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

这将打开一个交互式的变基会话.只需完全删除提交C,并保持D和E不变即可.然后,您将使D和E在不包含C的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.

这篇关于如何在Git中的分支之间移动提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆