git 给定一个提交 id,如何确定当前分支是否包含提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43535132/
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-09 04:56:15  来源:igfitidea点击:

Given a commit id, how to determine if current branch contains the commit?

git

提问by TieDad

What I'm trying to do is a version check. I want to ensure the code stays on top of a minimum version. So I need a way to know if the current branch contains a specified commit.

我想做的是版本检查。我想确保代码保持在最低版本之上。所以我需要一种方法来知道当前分支是否包含指定的提交。

回答by JiriS

There are multiple ways to achieve this result. First naive option is to use git logand search for a specific commit using grep, but that is not always precise

有多种方法可以实现这一结果。第一个天真的选择是使用git log和搜索特定的提交grep,但这并不总是准确的

git log | grep <commit_id>

You are better off to use git branchdirectly to find all branches containing given COMMIT_IDusing

您最好git branch直接使用来查找包含给定COMMIT_ID使用的所有分支

git branch --contains $COMMIT_ID

The next step is finding out current branch which can be done since git 1.8.1using

下一步是找出自git 1.8.1使用以来可以完成的当前分支

git symbolic-ref --short HEAD

And combined together as

并结合在一起作为

git branch $(git symbolic-ref --short HEAD) --contains $COMMIT_ID

But the command above doesn't return true or false and there is a shorter version that returns exit code 0 if commit is in current branch OR exit code 1 if not

但是上面的命令不返回真或假,并且有一个较短的版本,如果提交在当前分支中,则返回退出代码 0,否则返回退出代码 1

git merge-base --is-ancestor $COMMIT_ID HEAD

Exit code is nice, but as you want string trueor falseas answer you need to add a bit more and then combined with iffrom bash you get

退出代码很好,但是当您想要字符串truefalse作为答案时,您需要添加更多内容,然后与iffrom bash结合使用

if [ 0 -eq $(git merge-base --is-ancestor $COMMIT_ID HEAD) ]; then echo "true"; else echo "false"; fi

回答by Sajib Khan

Get a list of branch(es) that contains the specific commit.

获取包含特定提交的分支列表。

# get all the branches where the commit exists
$ git branch --contains <commit-id>

Check if a branch has the specific commit.

检查分支是否具有特定的提交。

# output the branch-name if the commit exists in that branch
$ git branch --contains <commit-id> | grep <branch-name>


Search the branch (say, feature) with exact matching.

搜索feature具有完全匹配的分支(例如,)。

$ git branch --contains <commit-id> | grep -E '(^|\s)feature$'

e.g.If you have 3 local branches called feature, feature1, feature2then

例如,如果您有 3 个名为feature, 的本地分支feature1feature2

$ git branch --contains <commit-id> | grep 'feature'

# output
feature
feature1
feature2

$ git branch --contains <commit-id> | grep -E '(^|\s)feature$'

# output
feature     

You can also search in both localand remotebranches (use -a) or only in remotebranches (use -r).

您还可以同时搜索localremote分支(使用-a)或仅搜索remote分支(使用-r)。

# search in both 'local' & 'remote' branches  
$ git branch -a --contains <commit-id> | grep -E '(^|\s)feature$'

# search in 'remote' branches  
$ git branch -r --contains <commit-id> | grep -E '(^|\s)feature$'

回答by Zitrax

Extracted comment by @torek as answer:

@torek 提取的评论作为答案:

See the proposed duplicate for how to find all branches that contain a specified commit.

请参阅建议的副本以了解如何查找包含指定提交的所有分支。

To find out if the currentbranch contains commit C, use the "plumbing" command git merge-base --is-ancestor. The current branch contains C if C is an ancestor of HEAD, so:

要确定当前分支是否包含提交 C,请使用“管道”命令git merge-base --is-ancestor。如果 C 是 HEAD 的祖先,则当前分支包含 C,因此:

if git merge-base --is-ancestor $hash HEAD; then
    echo I contain commit $hash
else
    echo I do not contain commit $hash
fi

(Side note: in shell scripts, a command that exits zero is "true" while one that exits nonzero is "false".)

(旁注:在 shell 脚本中,退出零的命令是“真”,而退出非零的命令是“假”。)

回答by logicor

Yeah another alternative:

是的,另一种选择:

git rev-list <branch name> | grep `git rev-parse <commit>`

This works best for me since it would also work on locally cached remote branches such as remotes/origin/master, on which git branch --containswon't work.

这对我来说效果最好,因为它也适用于本地缓存的远程分支,例如remotes/origin/master,在这些分支上git branch --contains不起作用。

This covers more than OP's question about just "current branch" but I find it dumb to ask a "any branch" version of this question so I decide to post here anyway.

这不仅涵盖了 OP 关于“当前分支”的问题,但我发现问这个问题的“任何分支”版本很愚蠢,所以我还是决定在这里发帖。

回答by hgrey

To list local branches containing commit:

列出包含提交的本地分支:

git branch --contains <commit-id>

git branch --contains <commit-id>

and to list all branches, including remote only, containing commit:

并列出包含提交的所有分支,包括仅远程分支:

git branch -a --contains <commit-id>

git branch -a --contains <commit-id>

Similarly to check if commit is in particular branch:

与检查提交是否在特定分支类似:

git log <branch> | grep <commit_id>

git log <branch> | grep <commit_id>

and if branch does not exist locally prefix branch name with origin/

如果分支不存在本地前缀分支名称 origin/

回答by DreamUth

git branch --contains <commit-id> --points-at <target branch name>

It will return the target branch name if the commit id exists in that branch. Otherwise the command will fail.

如果该分支中存在提交 ID,它将返回目标分支名称。否则命令将失败。

回答by Bruno Bronosky

Since this question has some nice scripty answers, I'm adding my favorites.

由于这个问题有一些很好的脚本答案,我添加了我的最爱。

This one will show you, for last $ncommits in $brwhich branches contains each:

这将向您展示,对于分支包含每个$n提交的最后一次提交$br

br=mybranch
n=10
git log --oneline -n$n $br | awk '{print; system("git branch --contains ")}'

This one is similar but does the same for a list of branches:

这个是类似的,但对分支列表的作用相同:

br="mybranch yourbranch herbranch"
n=4
for br in $brs; do git log --oneline -n$n $br | awk '{print; system("git branch --contains ")}'; done