如何将某些提交移动到git中的另一个分支? [英] How to move certain commits to another branch in git?
问题描述
情况:
- master在X处;
- quickfix1在X + 2处提交
然后我开始研究quickfix2,但不小心把quickfix1作为源分支来复制,而不是主分支。现在quickfix2在X + 2提交+2相关提交。
现在我想用quickfix2创建一个分支,但没有属于quickfix1的两个提交。 p>
我试图从quickfix2的某个版本创建补丁,但补丁不保存提交历史记录。有没有一种方法可以保存我的提交历史记录,但是在quickfix1中没有改动的分支? 解决方案
这是一个经典的案例 rebase --onto
:
#让我们前往当前的master(X,其中quickfix2应该开始)
git checkout master
#每次提交后重放* quickfix1之后直至quickfix2 HEAD。
git rebase --onto master quickfix1 quickfix2
所以你应该从
ooX(master HEAD)
\
q1a - q1b(quickfix1 HEAD)
\ $
q2a - q2b(quickfix2 HEAD)
到:
q2a' - q2b'(新的quickfix2 HEAD)
/
ooX(master HEAD)
\
q1a - q1b(quickfix1 HEAD)
最好在干净的工作树上完成。 >
请参阅 git config --global rebase.autostash true
,尤其是 Git 2.10之后。
The situation:
- master is at X;
- quickfix1 is at X + 2 commits
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.
Now I want to have a branch with quickfix2, but without the 2 commits that belong to quickfix1.
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?
This is a classic case of 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中的另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!