[英]How to move certain commits to be based on another branch in git?
情况:
这样:
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
然后我开始研究quickfix2,但不小心将quickfix1作为源分支进行复制,而不是master。 现在quickfix2处于X + 2提交+ 2相关提交。
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
\
q2a--q2b (quickfix2 HEAD)
现在我希望有一个quickfix2分支,但没有属于quickfix1的2次提交。
q2a'--q2b' (quickfix2 HEAD)
/
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
我尝试从quickfix2中的某个修订版创建补丁,但补丁不保留提交历史记录。 有没有办法保存我的提交历史记录,但有一个分支没有更改quickfix1?
这是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
所以你应该去
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
\
q2a--q2b (quickfix2 HEAD)
至:
q2a'--q2b' (new quickfix2 HEAD)
/
o-o-X (master HEAD)
\
q1a--q1b (quickfix1 HEAD)
最好在干净的工作树上完成。
请参阅git config --global rebase.autostash true
,尤其是在Git 2.10之后 。
您可以使用git cherry-pick
来选择要复制的提交。
可能最好的方法是从master创建分支,然后在该分支中使用git cherry-pick
来自你想要的quickfix2的2次提交。
你可以做的最简单的事情是挑选一个范围。 它与rebase --onto
一样,但对眼睛来说更容易:)
git cherry-pick quickfix1..quickfix2
我相信它是:
git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2
我发现的唯一工作方式:
git show>~/some_file.txt
git checkout master
git checkout -b new_branch
git apply -stat ~/some_file.txt
git apply -check ~/some_file.txt
git apply ~/some_file.txt
然后你需要再次提交更改,但手动操作要容易得多......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.