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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1419623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:02:35  来源:igfitidea点击:

How to list branches that contain a given commit?

gitversion-control

提问by Andrew Arnott

How can I query git to find out which branches contain a given commit? gitkwill 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.

如何查询 git 以找出哪些分支包含给定的提交?gitk通常会列出分支,除非有太多分支,在这种情况下它只会说“很多(38)”或类似的东西。我需要知道完整列表,或者至少某些分支是否包含提交。

回答by VonC

From the git-branch manual page:

git-branch 手册页

 git branch --contains <commit>

Only list branches which contain the specified commit (HEAD if not specified). Implies --list.

仅列出包含指定提交的分支(如果未指定,则为 HEAD)。暗示--list



 git branch -r --contains <commit>

Lists remote tracking branchesas well (as mentioned in user3941992's answerbelow) that is "local branches that have a direct relationship to a remote branch".

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



See also this git readyarticle.

另请参阅此git 就绪文章。

The --containstag 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.

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


Note: if the commit is on a remote tracking branch, add the -aoption.
(as MichielBcomments below)

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

git branch -a --contains <commit>


MatrixFrogcomments that it only shows which branches contain that exactcommit.
If you want to know which branches contain an "equivalent" commit (i.e. which branches have cherry-picked that commit) that's git cherry:

MatrixFrog评论说它只显示哪些分支包含确切的提交。
如果你想知道哪些分支包含一个“等效”提交(即哪些分支已经挑选了那个提交),那就是git cherry

Because git cherrycompares the changeset rather than the commit id (sha1), you can use git cherryto 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.

因为git cherry比较的是变更集而不是提交 id (sha1),您可以使用它git cherry来确定您在本地所做的提交是否已<upstream>在不同的提交 id 下应用。
例如,如果您<upstream>通过电子邮件提供补丁而不是直接推送或拉取提交,则会发生这种情况。

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

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

(在这里,标记为 ' -'的提交不会出现在 中git cherry,这意味着它们已经存在于 中<upstream>。)

回答by Eugen Konkov

You may run:

你可以运行:

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

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

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

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