Use -C
作为 git 的第一个参数:
git -C /home/repo log
Per the docs https://git-scm.com/docs/git,这样做的效果是:
-C <path>
运行就像 git 启动一样<path>
而不是当前工作目录。 ...
This is almost相当于--git-dir
and --work-tree
不附加通常的.git
文件夹。然而,选项--git-dir
and --work-tree
不存在从工作树外部访问存储库;它们用于移动.git
在其他地方,在某些情况下它们的使用要复杂得多。
例如,要获取日志/home/repo/subdir
only:
git -C /home/repo/subdir log .
or
git -C /home/repo log subdir
无法使用log .
with --git-dir
or --work-tree
。必须处理该路径以提取相对于工作树顶部的子路径,即使在这种情况下,如果不使用 git 也不会将其识别为路径--
选项,所以唯一可能的方法是:
git --git-dir /home/repo/.git log -- subdir
此外,--work-tree
根本不起作用log
子命令与我的版本(git 1.9.1)。它只是被忽略:
git --git-dir /home/repo/.git --work-tree /home/repo/subdir log -- subdir
git --git-dir /home/repo/.git --work-tree /home/repo/whatever log -- subdir
我什至不明白这是一个错误还是一个功能......像往常一样,有许多 git 设计选择。