如何将某些提交移动到基于 git 中的另一个分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2369426/
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 to move certain commits to be based on another branch in git?
提问by Alex Yarmula
The situation:
情况:
- master is at X
- quickfix1 is at X + 2 commits
- 主人在X
- quickfix1 在 X + 2 次提交
Such that:
这样:
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
Then I started working on quickfix2, but by accident took quickfix1 as the source branch to copy, not the master. Now quickfix2 is at X + 2 commits + 2 relevant commits.
然后我开始研究 quickfix2,但不小心将 quickfix1 作为要复制的源分支,而不是主分支。现在 quickfix2 处于 X + 2 次提交 + 2 次相关提交。
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
\
q2a--q2b (quickfix2 HEAD)
Now I want to have a branch with quickfix2, but without the 2 commits that belong to quickfix1.
现在我想要一个带有 quickfix2 的分支,但没有属于 quickfix1 的 2 个提交。
q2a'--q2b' (quickfix2 HEAD)
/
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
I tried to create a patch from a certain revision in quickfix2, but the patch doesn't preserve the commit history. Is there a way to save my commit history, but have a branch without changes in quickfix1?
我试图从 quickfix2 中的某个修订版创建补丁,但该补丁不保留提交历史。有没有办法保存我的提交历史,但在 quickfix1 中有一个没有更改的分支?
采纳答案by VonC
This is a classic case of rebase --onto
:
这是一个经典案例rebase --onto
:
# let's go to current master (X, where quickfix2 should begin)
git checkout master
# replay every commit *after* quickfix1 up to quickfix2 HEAD.
git rebase --onto master quickfix1 quickfix2
So you should go from
所以你应该从
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
\
q2a--q2b (quickfix2 HEAD)
to:
到:
q2a'--q2b' (new quickfix2 HEAD)
/
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
This is best done on a clean working tree.
See git config --global rebase.autostash true
, especially after Git 2.10.
这最好在干净的工作树上完成。
看git config --global rebase.autostash true
,尤其是在 Git 2.10 之后。
回答by DJ.
You can use git cherry-pick
to just pick the commit that you want to copy over.
您可以使用git cherry-pick
来选择要复制的提交。
Probably the best way is to create the branch out of master, then in that branch use git cherry-pick
on the 2 commits from quickfix2 that you want.
可能最好的方法是从 master 创建分支,然后在该分支中使用git cherry-pick
您想要的来自 quickfix2 的 2 个提交。
回答by Christoph
The simplest thing you can do is cherry picking a range. It does the same as the rebase --onto
but is easier for the eyes :)
您可以做的最简单的事情是挑选一个范围。它的作用与 相同,rebase --onto
但对眼睛更容易:)
git cherry-pick quickfix1..quickfix2
回答by Matthew Flaschen
I believe it's:
我相信是:
git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2
回答by Gopher
// on your branch that holds the commit you want to pass
$ git log
// copy the commit hash found
$ git checkout [branch that will copy the commit]
$ git reset --hard [hash of the commit you want to copy from the other branch]
// remove the [brackets]
Other more useful commands here with explanation: Git Guide
其他更有用的命令和解释:Git Guide