我怎样才能看到另一个分支是从哪个分支分叉出来的?
我的 git 存储库有三个分支:devel
、stable
和 customers/acme_patches
。很久以前,stable
是从 devel
分叉出来的,所有的错误修复都发生在 stable
中。 stable
时不时地会合并回 devel
。 customers/acme_patches
是一个包含一些特定于客户的补丁的分支。该分支未合并到 devel
和 stable
中。
用一点 ASCII 艺术来说明这个场景:
o---o---o customers/acme_patches? / o---o---1---o---o---o stable / \ \ o---o---o---2---o---o---o---o devel \ o---o---o customers/acme_patches?
现在我想知道:
customers/acme_patches
是从 devel
或 stable
分叉出来的?我只知道它过去是从其中一个分叉出来的,但我不知道是哪一个。例如,上图中可能是提交 1
或 2
。
我一直在使用 git log --oneline --graph
和 gitk
但自从 customers/acme_patches
在几百次提交前被分叉,很难遵循所绘制的线条。
是否有一个快速命令(一个小脚本也可以)可以以某种方式向后跟踪 customers/acme_patches
中的提交,找到带有两个子项(分叉点)的第一个提交,然后确定是否该提交是在 stable
中还是在 devel
中完成的?
在最好的情况下,我可以执行类似的命令(请原谅提示,我使用的是 Windows):
C:\src> git fork-origin customers/acme_patches
stable
My git repository has three branches, devel
, stable
and customers/acme_patches
. A long time ago, stable
was forked from devel
, and all the bugfixing takes place in stable
. Every now and then, stable
is merged back into devel
. customers/acme_patches
is a branch with a few customer-specific patches. The branch wasn't merged into either of devel
and stable
.
A bit of ASCII art to illustrate the scenario:
o---o---o customers/acme_patches? / o---o---1---o---o---o stable / \ \ o---o---o---2---o---o---o---o devel \ o---o---o customers/acme_patches?
Now I wonder:
What branch was customers/acme_patches
forked from - devel
or stable
? I only know that it was forked off one of them in the past, but I don't know which. E.g. it might have been commit 1
or 2
in the above diagram.
I've been playing around with git log --oneline --graph
and gitk
but since customers/acme_patches
was forked a few hundred commits ago, it's hard to follow the lines being drawn.
Is there maybe a quick command (a little script is fine, too) which can somehow follow the commits in customers/acme_patches
backwards to find the first commit with two children (the fork point) and then determines whether that commit was done in stable
or in devel
?
In the best case, I could just execute something like (excuse the prompt, I'm on Windows):
C:\src> git fork-origin customers/acme_patches
stable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 git 1.9/2.0(2014 年第 1 季度),您可以使用 git merge-base --fork-point 来根据 Git 请求最佳共同祖先。
您可以看到该新选项:
自从 提交来自 John Keeping (
johnkeeping
)、git rebase
可以使用相同的新--fork-point
选项,如果您需要将customers/acme_patches
等分支重新设置为devel
,该选项会派上用场。(我并不是说这在您的特定场景中有意义)
注意:Git 2.16(2018 年第一季度)确实澄清并增强了“
merge-base --fork-point
”的文档,因为它很清楚它计算了什么,但没有计算原因/目的。请参阅 提交 6d1700b(2017 年 11 月 9 日),作者:Junio C Hamano (
gitster
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 022dd4a,2017 年 11 月 27 日)因此,文档现在如下:
With git 1.9/2.0 (Q1 2014), you can use
git merge-base --fork-point
to ask for the best common ancestor according to Git.You can see that new option:
And since commit ad8261d from John Keeping (
johnkeeping
),git rebase
can use that same new--fork-point
option, which can come in handy should you need to rebase a branch likecustomers/acme_patches
ontodevel
.(I am not saying this would make sense in your specific scenario)
Note: Git 2.16 (Q1 2018) does clarify and enhance documentation for "
merge-base --fork-point
", as it was clear what it computed but not why/what for.See commit 6d1700b (09 Nov 2017) by Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 022dd4a, 27 Nov 2017)So the documentation now reads:
好吧,这个答案可能没有完美的解决方案。我的意思是,在 git 中没有相当于 fork-origin 的东西(据我所知)。
由于
stable
分支已合并到devel
中,因此您的acme_patches
(从 1 开始)同时位于devel
和上>稳定
分支。你可能会做的是:
如果你有 stable 但没有 devel,或者有 devel 但不稳定,那么你就知道它来自哪里。
例如,在情况 2 中,您将拥有,
而在情况 1 中,您将拥有
因为它现在在两个分支上(因为从稳定版合并到开发版)
Well, there is probably no perfect solution to this answer. I mean there is no
fork-origin
equivalent in git (to my knowledge).Because the
stable
branch is merged intodevel
, youracme_patches
(from 1) is on bothdevel
andstable
branch.What you could possibly do is:
If you have stable and not devel, or devel and not stable, then you know where it comes from.
For example, in the case 2, you would have
while in case 1 you would have
As it's now on both branches (because of the merge from stable to dev)
好吧, git merge-basecustomers/acme_patches stable 应该显示这两个分支的共同祖先。
例如,您可以尝试 gitk --left-rightcustomers/acme_patches...stable (注意三个点!)。这将显示这些分支中而不是合并基础中的所有提交。使用
--left-right
将根据每个提交所在的分支用左箭头或右箭头标记每个提交 - 如果它们位于customers/acme_patches,则使用左箭头;如果它们处于稳定状态,则使用右箭头。可能还添加
--date-order
我发现有时有助于理解输出。(您可以将此语法与 git log --graph 结合使用,而不是与 gitk 结合使用,但恕我直言,在这种情况下,可视化图形显示有了很大的改进)。
well,
git merge-base customers/acme_patches stable
should show the common ancestor of those two branches.You could try, for instance,
gitk --left-right customers/acme_patches...stable
(note three dots!). This will show all the commits that are in those branches and not in the merge base. Using--left-right
will mark each commit with a left or right arrow according to which branch they are in- a left arrow if they are in customers/acme_patches and a right arrow if they are in stable.Possibly also add
--date-order
which I've found sometimes helps make sense of the output.(You can use this syntax with
git log --graph
rather thangitk
but imho this is a case where the visual graph display is a big improvement).不确定它是否涵盖所有情况,但这是我想出的功能:
这是测试它们的脚本(
git-upstream-branch-test.sh
):像这样使用它,
在哪里NUMBER 是 1 到 11 之间的数字,用于指定要测试的情况(拓扑)。
Not sure if it covers all cases, but here's the functions that I came up with:
And here's the script to test them (
git-upstream-branch-test.sh
):Use it like so,
Where NUMBER is a number from 1 to 11 to specify which case (topology) to test.