有没有一种方法可以在不在该存储库中的情况下对存储库执行 Git 命令?
例如这样的事情:git/home/repo log
?
请不要告诉我 cd
到它。我通过 exec
调用来执行此操作。
最佳答案
使用 -C
作为 git 的第一个参数:
git -C /home/repo log
根据 the docs ,这样的效果是:
-C <path>
Run as if git was started in
<path>
instead of the current working directory. ...
这几乎等同于--git-dir
和 --work-tree
不附加通常的 .git
文件夹。但是,选项 --git-dir
和 --work-tree
不存在从工作树外部访问存储库;它们用于移动 .git
其他地方,并且在某些情况下它们的使用要复杂得多。
例如获取/home/repo/subdir
的日志仅:
git -C /home/repo/subdir log .
或
git -C /home/repo log subdir
不可能使用log .
与 --git-dir
或 --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 设计选择一样。
关于git - 如何在不在存储库中的情况下执行 Git 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7149984/