所以我的历史是这样的:
o---o---o---o 主人
\
o---o---o
\
o B
所以,解释一下:
- 我有分支
A
,它是从master
开始的
- 我有从
A
开始的分支B
(只有 1 次提交)
我想要的是:
o---o---o---o 主人
\
o---o---o
\
o B
我所做的是:
1).
git checkout 一个
git rebase 大师
这导致了很多冲突,在花费大量时间修复之后,出现了以下历史记录:
o---o---o---o 主人
\
o---o---o
这正是我想要的。
(我不知道B
现在在哪里)
2).
在此之后,我做了很多压缩并更改了 A
上的提交顺序,以使历史看起来像我想要的那样。
3).
现在,我还想做的是:
git checkout B
git rebase A
但这似乎不起作用,我也不知道为什么。如果我执行 git log
,我会看到在执行第 1 步之前的提交。
此外,我遇到了与我在第 1 步中已经解决的相同数量的冲突。我花了很多时间做这件事,不想再做一次。
This example建议使用 --onto
,我这样做了:
git checkout B
git rebase --onto A
但这会完全删除 B
上的提交,并使 A
和 B
指向同一提交,即 上的最后一个提交A
.
我的问题是:我怎样才能有效地从 A rebase B,使 B 看起来像是从 A 开始? (一开始确实如此)。
我最好的猜测是我错误地使用了 --onto
。或者我应该使用其他东西(比如 cherry-pick
)。
最佳答案
如何有效地从 A rebase B,使 B 看起来像是从 A 开始的? 假设你想移动恰好一个提交:
git rebase --onto A B~ B
如果你想移动多于一个提交使用:
git rebase --onto A old_A B
剩下的答案。
B 的分支仍然存在(您可以检查它),但它的父级仍然是 A
之前的确切提交对象。
要查看我使用的图形表示:
git log --graph --decorate --all
查看所有分支以及它们相对于彼此的位置。
你原来拥有的:
o---o---o---o master
\
o---o---o A
\
o B
你现在拥有的:
o---o---o-----------o master
\ \
o---o---o(B~) o---o---o A
\
o B
在使用--onto
方面,你需要有一个起点和一个终点。
使用:
git rebase --onto [target] [rebasing stops] [rebasing head]
git rebase --onto A B~ B
你得到了什么:
o---o---o----------o master
\ \
o---o---o o---o---o A
(old_A) \
o B
[branch_name]~
表示分支的父提交
B~
是您不想更改的分支。 (恰好是旧的A
)
或者,如果 B 是唯一一个以 A 作为父级的提交,(即 B 是从 master 分支出来的提交链的末尾),你可以这样做
git checkout B
git rebase master
git checkout B~ # this is the commit before B (the A commit)
git branch -d A # remove the old A branch (it was rebased, and so is now invalid
git branch A # recreate the A branch on the commit that is based on the original A
关于git - 如何从重新建立分支的分支中重新建立分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31881885/