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


当前回答

列出包含commit的本地分支:

Git分支——包含<commit-id>

列出所有包含commit的分支,只包括remote:

Git分支-a——包含<commit-id>

类似地,检查commit是否在特定的分支中:

Git log <branch> | grep <commit_id>

如果分支不存在,则在分支名称前加上origin/

其他回答

因为这个问题有一些很好的答案,我添加了我最喜欢的。

这个会告诉你,对于$br中的最后$n次提交,哪个分支包含了每一个:

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

下面这个类似,但对分支列表执行相同的操作:

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

是的,另一个选择:

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

这对我来说是最好的,因为它也适用于本地缓存的远程分支,如remotes/origin/master,在git分支上——contains不会工作。

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

Git签出将返回到当前HEAD(当前文件)的分支,但不会签出所有分支。例如,如果你做了一个git检出master,代码将被检出,就像你在master分支上一样,但它不会检出该分支以外的任何文件进行更改。

列出包含commit的本地分支:

Git分支——包含<commit-id>

列出所有包含commit的分支,只包括remote:

Git分支-a——包含<commit-id>

类似地,检查commit是否在特定的分支中:

Git log <branch> | grep <commit_id>

如果分支不存在,则在分支名称前加上origin/

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

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

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

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

使用精确匹配搜索分支(例如,特征)。

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

例:如果你有3个本地分支,分别叫feature, feature1, feature2

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

# output
feature
feature1
feature2

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

# output
feature     

您也可以在本地和远程分支中搜索(使用-a),或者只在远程分支中搜索(使用-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$'