将两个分支之间的差异合并到第三个分支 [英] Merge diff between two branches to third branch
问题描述
假设我有两个分支, master
和 new_feature
我本来应该研究一个特定功能,我认为这个功能是 new_feature
的一部分,所以我检出了 specific_feature
分支出 new_feature
分支,就像这样
git checkout -b specific_feature
现在我在 specific_feature
分支,将 upstream / new_feature
合并为几次以获得远程更改。
<现在,我开始知道我的
specific_feature
应该已经从 master
而不是 new_feature
。 ( new_feature
分支尚未准备好被推送) 有没有办法让我的 specific_feature
分支和 new_feature
分支,并将这些更改应用于新分支,例如 specific_feature_master
(分支出主)?
这似乎是一个工作 git rebase --onto
:
git rebase --onto master new_feature specific_feature
只需要提交 请注意,您必须强制将specific_feature推送到上游(如果您之前已经推送它): 如果其他人已经拉动该分支并且正在工作,那么这可能是个问题 davidriod 正确地指出: 我认为这不会像OP合并几倍于<$如果 new_feature
master $ $ b
git push --force specific_feature
。
new_feature>
从来没有更新(只抓取,从不拉),那么它可能仍然有效。
如果 new_feature
)并且合并到 specific_feature
中,那么 rebase --onto
只会发挥自上次合并以来的最后几次提交:在这里,只有 z'
提交将被重播,而不是第一个 z
。
x - x - x
\
y - y - Y - y - y(new_feature)
\\
z - M - z' - z'(specific_feature)
而不是樱桃采摘,我会:
$ ul
specific_feature
出于 master
(和将 specific_feature
分支标记为 tmp
) Y
(在 specifc_feature
中合并)的最后一个 new_feature
code> specific_feature 分行即:
git checkout -b tmp specific_feature
git checkout -B specific_feature master
git merge $(git merge-base tmp new_feature)#合并Y
---------- M(specific_feature)
/ /
x - x - x /
\\ / /
y - y - y - y - y(new_feature)
\
z - M - z' - z'(tmp)
然后, rebase --onto
可以使用相同的共同祖先 Y
作为正确的基础:
git rebase --onto specific_feature $(git merge - base tmp new_feature)tmp
git分支-D tmp
---------- M - z'' - z''(specific_feature)
/ /
x - x - x /
\ /
y - y - Y - y - y(new_feature)
Assume I have two branches, master
and new_feature
I was supposed to work on a specific feature, I thought this feature would be part of new_feature
so, I checked out specific_feature
branch out of the new_feature
branch, like so
git checkout -b specific_feature
Now I did a lot development in this specific_feature
branch, merged upstream/new_feature
into it a couple of times to get the remote changes.
Now, I come to know that my specific_feature
should have been branched out of master
not new_feature
. (new_feature
branch is not ready to be pushed)
Is there a way I can take the diff between my specific_feature
branch and new_feature
branch, and apply those changes to new branch say specific_feature_master
(branched out of master)?
That seems a job for git rebase --onto
:
git rebase --onto master new_feature specific_feature
That will take only the commits after new_feature
up to specific_feature
HEAD, and replay them onto master
.
Note that you will then have to force push specific_feature to upstream (if you already pushed it before): git push --force specific_feature
.
That can be an issue if others have already pulled that branch and are working on it.
davidriod correctly points out that:
I don't think this will work as the OP as merged several times the
upstream/new_feature
branch in the specific_feature branch
If new_feature
was never updated (fetch only, never pull), then it might still work.
If new_feature
was updated (pull) and merged into specific_feature
, then the rebase --onto
would only play the last few commits since the last merge: here, only z'
commits would be replayed, not the first z
.
x--x--x
\
y--y--Y--y--y (new_feature)
\ \
z--M--z'--z' (specific_feature)
Instead of cherry-picking, I would:
- make
specific_feature
out ofmaster
(and mark thespecific_feature
branch astmp
) - merge
Y
(the lastnew_feature
commit which was merged inspecifc_feature
) into the newspecific_feature
branch
That is:
git checkout -b tmp specific_feature
git checkout -B specific_feature master
git merge $(git merge-base tmp new_feature) # that merges Y
----------M (specific_feature)
/ /
x--x--x /
\ /
y--y--Y--y--y (new_feature)
\ \
z--M--z'--z' (tmp)
Then the rebase --onto
can use that same common ancestor Y
as the correct base:
git rebase --onto specific_feature $(git merge-base tmp new_feature) tmp
git branch -D tmp
----------M--z''--z'' (specific_feature)
/ /
x--x--x /
\ /
y--y--Y--y--y (new_feature)
这篇关于将两个分支之间的差异合并到第三个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!