如何在 Git 的分支之间移动提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3710192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I move a commit between branches in Git?
提问by Chris Perkins
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?
如何将 C 从 D 和 E 下方拉出?
回答by mipadi
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 是 的尖端,branch-E
B 是branch-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 之上。