我正在处理两个(私有)功能分支。
a -- b -- c <-- Master
\ \
\ d -- e <-- Branch1
\
f -- g <-- Branch2
在这些分支上工作了一会儿后,我发现我需要从 Branch1 中的 Branch2 进行更改。我想将 Branch2 中的更改重新定位到 Branch1。我想结束以下内容:
a -- b -- c <-- Master
\
d -- e -- f -- g <-- Branch1
我很确定我需要将第二个分支重新定位到第一个分支,但我不完全确定正确的语法以及我应该检查哪个分支。
这个命令会产生预期的结果吗?
(Branch1)$ git rebase --onto Branch1 Branch2
git rebase -
。见my answer below
切换到 Branch2 git checkout Branch2 在 Branch1 更改之上应用当前(Branch2)更改,留在 Branch2: git rebase Branch1
这将使您在 Branch2 中获得所需的结果:
a -- b -- c <-- Master
\
d -- e <-- Branch1
\
d -- e -- f' -- g' <-- Branch2
您可以删除 Branch1。
注意:如果您在 Branch1
,您将使用 Git 2.0(2014 年第二季度)能够输入:
git checkout Branch2
git rebase -
请参阅 Brian Gesiak modocache
的 commit 4f40740:
rebase:允许上一个分支的“-”简写
教 rebase 与 checkout 和 merge 相同的简写来命名分支以 rebase 当前分支;也就是说,“-”表示“我们之前所在的分支”。
我知道你已经接受了答案,但是如果你想完全按照你的要求去做(在 Branch1 上添加 Branch2 的提交,同时留在 Branch1 上),你可以这样做:
git checkout Branch1
git cherry-pick master..Branch2
你最终会得到这个。
a -- b -- c <-- Master
\
d -- e -- f' -- g' <-- Branch1 (current)
\
f -- g <-- Branch2
这将挑选 Branch2 上的每个提交,以便将它们放到 Branch1 上。然后你可以删除Branch2。
git cherry-pick mybranch...origin/mybranch
将这些提交选择到当前分支。
git cherry-pick <current-branch>..<other-branch>
我知道你要求 Rebase,但我会选择 Cherry-Pick 我想从 Branch2 移动到 Branch1 的提交。这样,我就不需要关心何时从 master 创建了哪个分支,并且我可以更好地控制合并。
a -- b -- c <-- Master
\ \
\ d -- e -- f -- g <-- Branch1 (Cherry-Pick f & g)
\
f -- g <-- Branch2
不定期副业成功案例分享
git rebase
将重写 Branch1 历史记录,以便在从 Branch2 复制的更改之上拥有 Branch1 的更改。这将导致以下提交顺序a - b - f - g - c' - d' - e'
。