该分支是从哪个git分支签出的?

该分支是从哪个git分支签出的?,git,Git,可能重复: 我如何找到所讨论的分支从中分离出来的git分支的名称(如果有的话)?我们很容易认为master总是master,而myu分支总是myu分支,但事实并非如此。假设您在Github、windows、linux和office中拥有存储库 因此,您有8个不同的分支: github/master github/my_branch windows/master windows/my_branch linux/master linux/my_branch office/master office

可能重复:


我如何找到所讨论的分支从中分离出来的git分支的名称(如果有的话)?

我们很容易认为
master
总是
master
,而
myu分支
总是
myu分支
,但事实并非如此。假设您在Github、windows、linux和office中拥有存储库

因此,您有8个不同的分支:

github/master
github/my_branch
windows/master
windows/my_branch
linux/master
linux/my_branch
office/master
office/my_branch
作为人类,您将它们视为
master
myu分支
,但git将它们视为8个不同的分支。因此,如果您有这样一个网络:

------------------------------------------------ linux/master
 \--/ \-------/      /            \-------- office/my_branch
  | \---|--\--------/-------------------\------- my_branch
  |     |
  |   office/master
  | 
windows/master
                    /------------ master
------(master)-----
                    \------------ my_branch
问我的分支机构来自哪里是什么意思?这是许多分支合并的结果


我想告诉你们的是,你们的问题有一个哲学问题。然而,有一种方法可以回答这个问题,尽管不是完美的。首先让我们看一下git日志:

git log my_branch --pretty=oneline --graph
给你一个很好的介绍合并和东西。从git日志手册页:

--first-parent
    Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch,
    because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual
    commits brought in to your history by such a merge.
使用它,可以得到分支的线性历史记录。删除图形并仅输出SHA1,您将获得:

git log my_branch --pretty=format:"%H" --first-parent
使用以下命令,可以判断哪些分支包含SHA1:

git branch --contains <commit>
我说它不完美,因为下面的情况。想象一下,如果
myu分支
是从
master
分支而来的。事实上,你会看到这样的图表:

------------------------------------------------ linux/master
 \--/ \-------/      /            \-------- office/my_branch
  | \---|--\--------/-------------------\------- my_branch
  |     |
  |   office/master
  | 
windows/master
                    /------------ master
------(master)-----
                    \------------ my_branch

初始提交包含在两个分支的历史记录中。不知道他们最初是从师父那里来的。因此,此脚本将告诉您
my_branch
是从
master
分支的,同时告诉您
master
是从
my_branch
分支的。无法分辨哪个是原始分支。

您是指它正在跟踪的远程分支,还是分支从中分离出来的分支(如果有)?我不认为你总能找到后者。这家公司从当地分公司(如果有的话)。无法比您更好地说明这一点。我认为Git甚至没有记录这些信息,尽管我可能弄错了。请参见
Git brach--contains HEAD~X
,它显示了包含当前分支的提交HEAD~X的分支。如果我们循环这个命令,递增X,那么最后我们将找到包含在这个分支和我们正在寻找的分支中的提交。