如何在不进入存储库的情况下执行Git命令?


130

有没有一种方法可以对某个存储库执行Git命令,而无需进入该存储库?

例如是这样的:git /home/repo log

请不要告诉我cd。我正在通过exec电话这样做。


3
您不能仅将“ cd / home / repo && git log”作为您的exec调用吗?
亚历克斯·霍安斯基

我有一些奇怪的问题。我正在开始一个新的过程,不是一个真正的执行者,但是我不知道它们有什么不同。我发现只查找一个用于指定git目录的参数会更容易。



1
git --git-dir = / home / repo / .git日志
Atiq Ur Rehman

Answers:


101

尝试:

git --git-dir=/home/repo/.git log

重要的是,将路径一直到存储库的.git目录。否则,您只会收到一条错误消息,内容如下:

fatal: Not a git repository

2
我不断fatal: Not a git repository (or any of the parent directories): .git。@帕特里克我没有拒绝投票吗?

3
@帕特里克(Patrick)答案已被编辑(它原本是说--work-dir我认为的)
Gareth

1
这对我不起作用,因为它将提交的所有文件都显示为已删除。好像是在检查提交,而不是在检查文件夹内容。请参阅@calandoa的答案以获得更好的性能。
mxcd

2
使用最新的git不适用于我,但是使用-C选项有效,例如:git -C /home/repo/.git log
D.Snap

1
这实际上没有按预期的那样工作。为什么这是任何可接受的答案?这将使用当前目录作为工作树,并使用指定的.git作为历史记录,这是完全错误的。例如,如果您不只是显示日志git status-C是正确的方法。
Wang

218

使用-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-treelog我的版本(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设计选择一样。


感谢您添加其他说明。
Erick G. Hagstrom

git stash是子命令的另一个示例,如果--git-dir--work-tree被使用,它将失败。
d5ve '16

4
-C选项已添加到git的1.8.5版本中,该版本于2013
。– d5ve '16

-C对我来说比--git-dir更好。...- C支持仓库的git config设置。正在运行的Git版本1.9.5.msysgit.1
Straff

@calandoa,我们总是需要从根目录开始指定位置,如果存储库的名称包含多个单词,我们
还要再说一遍,

17

实际上,您需要一起使用--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

1
这比单独使用--git-dir的答案更正确。git-scm.com/blog/2010/04/11/environment.html
Erick G.

11

我已经尝试了很多次!我终于明白了!

git -C dir --no-pager log --format='%an' -1 filename

请记住,请不要将.git添加到您的

-C目录


3

这类似于@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


1

对于任何git命令,您都可以执行以下操作:

git --git-dir=<PATH-TO-REPO>/.git --work-tree=<PATH-TO-REPO> <git-command>

例如,如果要执行git status:

 git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  status

或者,如果您想检查仓库所在的分支:

git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  rev-parse --abbrev-ref HEAD
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.