这个分支是从哪个 git 分支检出的?

9

可能是重复问题:
查找分支的父分支

如果有的话,如何找到指定分支分离出的git分支的名称?


你是指它正在追踪的远程分支,还是从哪个分支分离出来的分支(如果有的话)?我认为你并不总是能找到后者。 - Fred Foo
这个分支分离出来的本地分支(如果有)。无法比你说得更好了。 - Dziamid
我认为Git甚至不记录那些信息,但是我可能错了。 - Fred Foo
4
请参见 https://dev59.com/LXA75IYBdhLWcg3wrrNS,该页面讨论如何查找一个分支的父分支。 - Timofey Stolbov
git branch --contains HEAD~X 这个命令可以显示包含当前分支的提交 HEAD~X 的分支。如果我们循环执行该命令,逐步增加 X 的值,最终就能找到一个被当前分支包含的提交以及我们正在寻找的分支。 - Dziamid
1个回答

5
很容易让我们认为master总是mastermy_branch总是my_branch,但事实并非如此。假设你的代码库在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

作为人类,您将它们视为mastermy_branch,但是git将它们视为8个不同的分支。因此,如果您有这样的网络:

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

询问my_branch来自哪里是什么意思?它是许多分支合并的结果!


所以,我想告诉你的是,你的问题存在一个哲学问题。然而,有一种方法可以回答它,虽然不完美。首先,让我们看看git log

git log my_branch --pretty=oneline --graph

这篇git-log手册可以让你更好地了解合并和相关内容的展示。以下是摘自man页面的内容:

--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>

使用这些命令来编写脚本,你可以使用以下脚本,基本上查找最新的SHA1值,该值包含在除你所感兴趣的分支之外的其他分支中。然后输出该分支。(注意:我对bash脚本不是很熟练,因此可能不太有效率):

#! /bin/bash

if [ $# -lt 1 ]; then
  branch=master
else
  branch=$1
fi

sha1s=$(git log $1 --pretty=format:"%H")
res="Doesn't branch from anything"

for i in $sha1s; do
  b=$(git branch --contains $i | awk '{ if (NF > 1) print $2; else print $1 }') # use awk to remove * from current branch
  other_branch="";
  for j in $b; do
    if [ $branch != $j ]; then
      other_branch=$j
      break;
    fi
  done
  if [ -n "$other_branch" ]; then
    res=$other_branch
    break
  fi
done

printf -- '%s\n' "$res"

我说这不是完美的,因为存在以下情况。想象一下如果my_branchmaster分支出来。实际上,您会看到如下图所示的图形:

                    /------------ master
------(master)-----
                    \------------ my_branch

最初的提交记录包含在两个分支的历史记录中。无法确定它们最初来自于主分支。因此,这个脚本会告诉你my_branch是从master分支创建的,并同时告诉你master也是从my_branch创建的。没有办法确定哪一个是原始的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接