git rev-list的branches与glob模式语法咨询及无输出故障排查
Git rev-list中--glob和--branches参数的正确用法
问题根源
根据Git官方规则:
假设refs/heads下的所有引用都在命令行中列出。若指定
,则将分支限制为匹配给定shell glob的分支。若模式不含?、或[,则默认在末尾添加/。
假设所有匹配shell glob的引用都在命令行中列出。若缺少开头的refs/,会自动添加。若模式不含?、或[,则默认在末尾添加/。
当你使用的模式不含任何通配符(?、*、[)时,Git会自动在模式末尾追加/*,导致匹配的是目标分支下的子引用(而非分支本身),而你的分支都是一级分支,没有子分支,因此无法匹配到,导致无输出。
针对你的场景的解决方案
1. 修复--branches=master的用法
要匹配master分支,你需要在模式中加入通配符,避免Git自动追加/*:
git rev-list --simplify-by-decoration --format='%D' --branches=master*
master*作为模式包含通配符*,Git会直接匹配refs/heads/master*,也就是master分支本身。
2. 修复--glob='refs/heads/my-test'的用法
如果要匹配my-test分支(或my-test2),同样需要加入通配符:
- 匹配
my-test和my-test2:
git rev-list --simplify-by-decoration --format='%D' --glob='refs/heads/my-test*'
- 仅精确匹配
my-test分支,可以使用更精准的glob模式:
git rev-list --simplify-by-decoration --format='%D' --glob='refs/heads/my-test[^/]'
[^/]表示匹配非/的字符,确保仅匹配my-test本身,不会匹配其子引用(如果有的话)。
额外说明
如果你需要精确匹配单个分支,也可以直接将分支引用作为命令参数传入,无需使用--glob或--branches:
# 精确匹配master分支 git rev-list --simplify-by-decoration --format='%D' refs/heads/master # 精确匹配my-test分支 git rev-list --simplify-by-decoration --format='%D' refs/heads/my-test
内容的提问来源于stack exchange,提问作者Wang
相关产品推荐
相关产品推荐

