如何正确使用git log --remotes查看指定远程分支的提交日志?
git log --remotes=origin/master无输出的问题 可行方案
方案1:在模式中加入通配符*
执行以下命令即可正确输出origin/master的提交日志:
git log --remotes=origin/master*
原理:当--remotes的模式包含*、?或[]这类通配符时,Git不会自动在模式末尾追加/*,*可以匹配空字符串,因此能精准匹配到origin/master这个远程跟踪分支。
方案2:使用--glob选项直接指定完整ref路径
如果觉得通配符的写法不够直观,也可以用--glob选项直接指定远程分支的完整ref路径:
git log --glob=refs/remotes/origin/master
原理:--glob选项会直接匹配指定的ref路径,不会像--remotes那样对无通配符的模式做自动追加处理,因此能准确找到origin/master对应的ref文件。
方案3:保留位置参数并合理穿插选项
如果只是担心位置参数和--exclude选项的穿插问题,其实Git允许选项和位置参数混合使用,只要把--exclude放在位置参数之前即可,比如:
git log --exclude=some/path origin/master
或者用--分隔选项和路径参数(如果路径和选项冲突时更安全):
git log --exclude=some/path -- origin/master
这种方式不需要依赖--remotes选项,也能满足后续使用--exclude的需求。
问题原因回顾
根据git log --help的文档说明:
--remotes[=
]
Pretend as if all the refs in refs/remotes are listed on the
command line as. If is given, limit
remote-tracking branches to ones matching given shell glob. If
pattern lacks ?, , or [, / at the end is implied.
当执行git log --remotes=origin/master时,因为模式里没有通配符,Git会自动将其处理为git log --remotes=origin/master/*,但本地.git/refs/remotes/origin/master是一个文件而非目录,因此没有匹配到任何ref,导致无输出。
内容的提问来源于stack exchange,提问作者jamesdlin

