[英]How to move certain commits to be based on another branch in git?
The situation: 情况:
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. 然后我开始研究quickfix2,但不小心将quickfix1作为源分支进行复制,而不是master。 Now quickfix2 is at X + 2 commits + 2 relevant commits.
现在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. 我尝试从quickfix2中的某个修订版创建补丁,但补丁不保留提交历史记录。 Is there a way to save my commit history, but have a branch without changes in quickfix1?
有没有办法保存我的提交历史记录,但有一个分支没有更改quickfix1?
This is a classic case of rebase --onto
: 这是
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之后 。
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次提交。
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
I believe it's: 我相信它是:
git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2
The only working way that I found: 我发现的唯一工作方式:
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
Then you need to commit the changes again, but it's much easier then doing it manually... 然后你需要再次提交更改,但手动操作要容易得多......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.