如何列出包含给定提交的分支?
如何查询 git 以找出哪些分支包含给定的提交? gitk 通常会列出分支,除非分支太多,在这种情况下它只会说“很多(38)”或类似的东西。我需要知道完整的列表,或者至少知道某些分支是否包含提交。
How can I query git to find out which branches contain a given commit? gitk
will usually list the branches, unless there are too many, in which case it just says "many (38)" or something like that. I need to know the full list, or at least whether certain branches contain the commit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 git-branch 手册页 :
还列出远程跟踪分支(如上所述在 user3941992的答案如下),即“与远程分支有直接关系的本地分支”。
正如卡尔卡尔所指出的 Walsh,这仅适用于 默认refspec
如果您需要包含其他引用命名空间(拉取请求,Gerrit,...),您需要添加新的refspec,然后再次获取:
另请参阅此 git 准备就绪 文章。
注意:如果提交位于远程跟踪分支上,请添加
-a
选项。(如 MichielB 评论 下面)
MatrixFrog 评论说它只显示哪些分支包含该确切提交。
如果您想知道哪些分支包含“等效”提交(即哪些分支选择了该提交),则
gitcherry
:(在这里,标记为“
-
”的提交不会在gitcherry
中显示,这意味着它们已经存在于
中。 )From the git-branch manual page:
Lists remote tracking branches as well (as mentioned in user3941992's answer below) that is "local branches that have a direct relationship to a remote branch".
As noted by Carl Walsh, this applies only to the default refspec
If you need to include other ref namespace (pull request, Gerrit, ...), you need to add that new refspec, and fetch again:
See also this git ready article.
Note: if the commit is on a remote tracking branch, add the
-a
option.(as MichielB comments below)
MatrixFrog comments that it only shows which branches contain that exact commit.
If you want to know which branches contain an "equivalent" commit (i.e. which branches have cherry-picked that commit) that's
git cherry
:(Here, the commits marked '
-
' wouldn't show up withgit cherry
, meaning they are already present in<upstream>
.)您可以运行:
从输出中最后一次提交的注释中,您可以找到原始分支名称
示例:
You may run:
From comment of last commit in the output you may find original branch name
Example:
gitbranch -r --contains
的答案对于普通远程分支来说效果很好,但如果提交仅位于隐藏的head
命名空间中,GitHub 为 PR 创建,您还需要执行一些步骤。假设,如果 PR #42 来自已删除的分支,并且 PR 线程拥有对存储库上提交的唯一引用,则 gitbranch -r 不知道 PR #42,因为像
这样的引用默认情况下,refs/pull/42/head 未列为远程分支。
在
.git/config
中的[remote "origin"]
部分添加新行:(这个要点有更多上下文。)
然后当你
git fetch
时你会得到所有的PR分支,当你运行git时分支-r --contains
你会看到origin/pr/42
包含提交。The answer for
git branch -r --contains <commit>
works well for normal remote branches, but if the commit is only in the hiddenhead
namespace that GitHub creates for PRs, you'll need a few more steps.Say, if PR #42 was from deleted branch and that PR thread has the only reference to the commit on the repo,
git branch -r
doesn't know about PR #42 because refs likerefs/pull/42/head
aren't listed as a remote branch by default.In
.git/config
for the[remote "origin"]
section add a new line:(This gist has more context.)
Then when you
git fetch
you'll get all the PR branches, and when you rungit branch -r --contains <commit>
you'll seeorigin/pr/42
contains the commit.