Jir*_*riS 154
有多种方法可以实现这一结果.第一个选项天真选项是使用git log
和搜索特定的提交grep
,但这并不总是精确的
git log | grep <commit_id>
Run Code Online (Sandbox Code Playgroud)
您最好git branch
直接使用查找包含给定COMMIT_ID
使用的所有分支
git branch --contains $COMMIT_ID
Run Code Online (Sandbox Code Playgroud)
下一步是找出自git 1.8.1
使用以来可以完成的当前分支
git symbolic-ref --short HEAD
Run Code Online (Sandbox Code Playgroud)
并结合在一起
git branch $(git symbolic-ref --short HEAD) --contains $COMMIT_ID
Run Code Online (Sandbox Code Playgroud)
但上面的命令不返回true或false,如果commit在当前分支中,则返回退出代码0的较短版本如果不是,则退出代码1
git merge-base --is-ancestor $COMMIT_ID HEAD
Run Code Online (Sandbox Code Playgroud)
退出代码很不错,但是当你想要字符串true
或false
作为答案时,你需要添加更多,然后与if
bash 结合使用
if [ 0 -eq $(git merge-base --is-ancestor $COMMIT_ID HEAD) ]; then echo "true"; else echo "false"; fi
Run Code Online (Sandbox Code Playgroud)
Saj*_*han 64
获取包含特定提交的分支列表.
# get all the branches where the commit exists
$ git branch --contains <commit-id>
Run Code Online (Sandbox Code Playgroud)
检查分支是否具有特定提交.
# output the branch-name if the commit exists in that branch
$ git branch --contains <commit-id> | grep <branch-name>
Run Code Online (Sandbox Code Playgroud)
feature
使用完全匹配搜索分支(例如).
$ git branch --contains <commit-id> | grep -E '(^|\s)feature$'
Run Code Online (Sandbox Code Playgroud)
例如,如果你有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
Run Code Online (Sandbox Code Playgroud)
您还可以在同时进行搜索local
和remote
分支机构(使用-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$'
Run Code Online (Sandbox Code Playgroud)
hgr*_*rey 14
列出包含提交的本地分支:
git branch --contains <commit-id>
并列出所有包含提交的分支,仅包括远程分支:
git branch -a --contains <commit-id>
类似地检查提交是否在特定分支中:
git log <branch> | grep <commit_id>
如果本地不存在分支,则分支名称前缀为origin/
Pra*_*nha 14
仅在当地分支机构检查,已检查。
git branch --contains $COMMIT_ID
Run Code Online (Sandbox Code Playgroud)
签入所有分支(获取的本地和远程分支)
git branch -a --contains $COMMIT_ID
Run Code Online (Sandbox Code Playgroud)
确保获取遥控器
git fetch origin
Run Code Online (Sandbox Code Playgroud)
Zit*_*rax 12
@torek 提取的评论作为答案:
请参阅建议的副本以了解如何查找包含指定提交的所有分支。
要查明当前分支是否包含提交 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
Run Code Online (Sandbox Code Playgroud)
(旁注:在 shell 脚本中,退出零的命令为“true”,而退出非零的命令为“false”。)
是的,另一种选择:
git rev-list <branch name> | grep `git rev-parse <commit>`
Run Code Online (Sandbox Code Playgroud)
这对我来说效果最好,因为它也适用于本地缓存的远程分支,例如remotes/origin/master
,在这些分支上git branch --contains
不起作用。
这不仅涵盖了 OP 关于“当前分支”的问题,但我发现问这个问题的“任何分支”版本很愚蠢,所以我还是决定在这里发帖。
归档时间: |
|
查看次数: |
49569 次 |
最近记录: |