如何列出包含给定提交的分支?

发布于 2025-01-12 16:22:31 字数 105 浏览 2 评论 0原文

如何查询 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无言温柔 2025-01-19 16:22:32

来自 git-branch 手册页 :

 git branch --contains <commit>

仅列出包含指定提交的分支(如果未指定则为 HEAD)。暗示--list
(在 '--contains' 之前使用 '-a' 选项来查看您尚未签出的所有分支。例如 gitbranch -a --包含<提交>


 git branch -r --contains <commit>

还列出远程跟踪分支(如上所述在 user3941992答案如下),即“与远程分支有直接关系的本地分支”。


正如卡尔卡尔所指出的 Walsh,这仅适用于 默认refspec

fetch = +refs/heads/*:refs/remotes/origin/*

如果您需要包含其他引用命名空间(拉取请求Gerrit,...),您需要添加新的refspec,然后再次获取:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
git fetch
git branch -r --contains <commit>

另请参阅此 git 准备就绪 文章。

--contains 标签将确定某个提交是否已引入您的分支。也许您已经从您认为已应用的补丁中获得了提交 SHA,或者您只是想检查您最喜欢的开源项目是否已提交,该项目可将内存使用量减少 75%。

$ git log -1 tests
commit d590f2ac0635ec0053c4a7377bd929943d475297
Author: Nick Quaranto <[email protected]>
Date:   Wed Apr 1 20:38:59 2009 -0400

    Green all around, finally.

$ git branch --contains d590f2
  tests
* master

注意:如果提交位于远程跟踪分支上,请添加-a选项
(如 MichielB 评论 下面

git branch -a --contains <commit>

MatrixFrog 评论说它只显示哪些分支包含该确切提交。
如果您想知道哪些分支包含“等效”提交(即哪些分支选择了该提交),则 gitcherry

因为 gitcherry 比较变更集而不是提交 id (sha1),因此您可以使用 gitcherry 来查明是否有提交您在本地制作的内容已在不同的提交 ID 下应用
例如,如果您通过电子邮件提供补丁而不是直接推送或拉取提交,就会发生这种情况。

           __*__*__*__*__> <upstream>
          /
fork-point
          \__+__+__-__+__+__-__+__> <head>

(在这里,标记为“-”的提交不会在 gitcherry 中显示,这意味着它们已经存在于 中。 )

From the git-branch manual page:

 git branch --contains <commit>

Only list branches which contain the specified commit (HEAD if not specified). Implies --list.
(Use '-a' option before '--contains' to look in all branches that you haven't checked out. e.g. git branch -a --contains <commit>)


 git branch -r --contains <commit>

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

fetch = +refs/heads/*:refs/remotes/origin/*

If you need to include other ref namespace (pull request, Gerrit, ...), you need to add that new refspec, and fetch again:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
git fetch
git branch -r --contains <commit>

See also this git ready article.

The --contains tag will figure out if a certain commit has been brought in yet into your branch. Perhaps you’ve got a commit SHA from a patch you thought you had applied, or you just want to check if commit for your favorite open source project that reduces memory usage by 75% is in yet.

$ git log -1 tests
commit d590f2ac0635ec0053c4a7377bd929943d475297
Author: Nick Quaranto <[email protected]>
Date:   Wed Apr 1 20:38:59 2009 -0400

    Green all around, finally.

$ git branch --contains d590f2
  tests
* master

Note: if the commit is on a remote tracking branch, add the -a option.
(as MichielB comments below)

git branch -a --contains <commit>

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:

Because git cherry compares the changeset rather than the commit id (sha1), you can use git cherry to find out if a commit you made locally has been applied <upstream> under a different commit id.
For example, this will happen if you’re feeding patches <upstream> via email rather than pushing or pulling commits directly.

           __*__*__*__*__> <upstream>
          /
fork-point
          \__+__+__-__+__+__-__+__> <head>

(Here, the commits marked '-' wouldn't show up with git cherry, meaning they are already present in <upstream>.)

酒与心事 2025-01-19 16:22:32

您可以运行:

git log <SHA1>..HEAD --ancestry-path --merges

从输出中最后一次提交的注释中,您可以找到原始分支名称

示例:

       c---e---g--- feature
      /         \
-a---b---d---f---h---j--- master

git log e..master --ancestry-path --merges

commit h
Merge: g f
Author: Eugen Konkov <>
Date:   Sat Oct 1 00:54:18 2016 +0300

    Merge branch 'feature' into master

You may run:

git log <SHA1>..HEAD --ancestry-path --merges

From comment of last commit in the output you may find original branch name

Example:

       c---e---g--- feature
      /         \
-a---b---d---f---h---j--- master

git log e..master --ancestry-path --merges

commit h
Merge: g f
Author: Eugen Konkov <>
Date:   Sat Oct 1 00:54:18 2016 +0300

    Merge branch 'feature' into master
在巴黎塔顶看东京樱花 2025-01-19 16:22:32

gitbranch -r --contains 的答案对于普通远程分支来说效果很好,但如果提交仅位于隐藏的 head 命名空间中,GitHub 为 PR 创建,您还需要执行一些步骤。

假设,如果 PR #42 来自已删除的分支,并且 PR 线程拥有对存储库上提交的唯一引用,则 gitbranch -r 不知道 PR #42,因为像 这样的引用默认情况下,refs/pull/42/head 未列为远程分支。

.git/config 中的 [remote "origin"] 部分添加新行:

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

(这个要点有更多上下文。)

然后当你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 hidden head 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 like refs/pull/42/head aren't listed as a remote branch by default.

In .git/config for the [remote "origin"] section add a new line:

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

(This gist has more context.)

Then when you git fetch you'll get all the PR branches, and when you run git branch -r --contains <commit> you'll see origin/pr/42 contains the commit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文