git - 如何在不在存储库中的情况下执行 Git 命令?

标签 git

有没有一种方法可以在不在该存储库中的情况下对存储库执行 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/

相关文章:

git - 为什么 "git pull origin master"实际上并没有更改我计算机目录中的文件?

git - 在 Git 存储库中,如何正确重命名目录?

django - 如何将git repo的子目录部署到elastic beanstalk

git - 如何在所有克隆/机器上禁用 Git 行尾(CRLF 到 LF)?

git - Qt Creator Git, checkout 命令

c++ - 完全替换功能时是否有可能获得清晰的 git diff?

git - 我如何简单地分支/复制 master 并将此分支上传到 GitHub?

git - 如何禁止 git commit -a 选项

git - 为什么没有 git 版本目录?

git - 加入同一存储库旧版本的历史记录