有没有一种方法可以对某个存储库执行Git命令,而无需进入该存储库?
例如是这样的:git /home/repo log?
请不要告诉我cd。我正在通过exec电话这样做。
有没有一种方法可以对某个存储库执行Git命令,而无需进入该存储库?
例如是这样的:git /home/repo log?
请不要告诉我cd。我正在通过exec电话这样做。
Answers:
尝试:
git --git-dir=/home/repo/.git log
重要的是,将路径一直到存储库的.git目录。否则,您只会收到一条错误消息,内容如下:
fatal: Not a git repository
fatal: Not a git repository (or any of the parent directories): .git。@帕特里克我没有拒绝投票吗?
--work-dir我认为的)
git status。-C是正确的方法。
使用-C选项(docs):
git -C /home/repo log
这几乎等同于--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 stash是子命令的另一个示例,如果--git-dir和--work-tree被使用,它将失败。
实际上,您需要一起使用--git-dir和--work-tree。这是一个例子:
local [] Desktop: mkdir git
local [] Desktop: cd git
local [] git: touch README.txt
local [] git: git init
Initialized empty Git repository in /Users/albert/Desktop/git/.git/
local [] git: cd ..
local [] Desktop: git --work-tree=git --git-dir=git/.git add .
local [] Desktop: git --work-tree=git --git-dir=git/.git commit -a -m 'initial commit, called from outside the git directory'
[master (root-commit) ee951b1] initial commit, called from outside the git directory
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.txt
local [] Desktop: cd git
local [] git: git log --pretty=oneline
ee951b161053e0e0948f9e2a36bfbb60f9c87abe initial commit, called from outside the git di
这类似于@max的答案。不幸的是,--git-dir没有满足我的要求。编辑回想起来,另一个[上一页]回答我没有读过建议使用--work-tree。我不确定使用环境还是标志更合适,所以我会留下答案,以防有人在其中使用,但是我将切换为使用--work-tree/ --git-dir。
有两个存储库,一个在另一个内,但没有子模块。外部仓库会忽略它:
tools (outer repo)
gcc/4.9.2 (inner repo)
我想要的是git rev-parse --show-prefix相对于外部仓库的输出。这是我想出的(bash语法;为了便于阅读而分行):
prefix_dir=$(cd ..; git rev-parse --show-toplevel)
prefix_dir=$(GIT_WORK_TREE=${prefix_dir} git rev-parse --show-prefix)
从4.9.2内部执行时,将生成字符串gcc/4.9.2。