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

Tow*_*wer 99 git

有没有办法在没有存储库的情况下对存储库执行Git命令?

比如这样的事情:git /home/repo log

请不要告诉我cd.我是通过exec电话这样做的.

cal*_*doa 180

使用-C选项(docs):

git -C /home/repo log
Run Code Online (Sandbox Code Playgroud)

几乎等同于--git-dir--work-tree不附加平常.git文件夹

编辑:

回答downvoted ......非常惊人,严格来说,这是这个问题的唯一正确答案.选项--git-dir--work-tree不存在从工作树之外访问该存储库,它们被用于移动.git别的地方,他们要复杂得多在某些情况下使用.

例如,要/home/repo/subdir仅获取日志:

git -C /home/repo/subdir log .
Run Code Online (Sandbox Code Playgroud)

要么

git -C /home/repo log subdir
Run Code Online (Sandbox Code Playgroud)

这是不可能的使用log .--git-dir--work-tree.必须处理路径以提取相对于工作树顶部的子路径,即使在这种情况下,如果不使用该--选项,git也不会将其识别为路径,因此唯一可行的方法是:

git --git-dir /home/repo/.git log -- subdir
Run Code Online (Sandbox Code Playgroud)

此外,使用我的版本(git 1.9.1)的子命令--work-tree完全不起作用log.它被忽略了:

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
Run Code Online (Sandbox Code Playgroud)

我甚至不知道这是一个错误还是一个功能......像往常一样有很多git设计选择.

  • -C选项是在git的1.8.5版本中添加的,该版本于2013年发布. (3认同)
  • 注意:您需要将 -C 选项传递给 git 而不是子命令(正如@calandoa 在此答案中正确完成的那样)。调用 `git -C /some/dir add .` 会按预期工作,而 `git add -C /some/dir .` 会抛出错误。只是需要注意以防止混淆结果。 (3认同)

max*_*max 79

尝试:

git --git-dir=/home/repo/.git log
Run Code Online (Sandbox Code Playgroud)

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

fatal: Not a git repository
Run Code Online (Sandbox Code Playgroud)

  • 使用最新的 git 对我来说不起作用,但是使用 -C 选项它可以工作,例如: git -C /home/repo/.git log (9认同)
  • 这实际上并没有像 PO 预期的那样起作用。为什么这是任何可接受的答案?这将使用当前目录作为工作树并使用指定的 .git 作为历史记录,这是完全错误的。如果您不只显示日志,例如“git status”。`-C` 是正确的方法。 (9认同)
  • 这对我不起作用,因为它显示提交的所有文件都已删除。似乎它正在检查提交而不是文件夹内容。请参阅@calandoa 的答案以获得更好的性能。 (6认同)
  • @Patrick答案被编辑了(我原本说' - work-dir`我认为) (3认同)
  • 我一直变得"致命:不是git存储库(或任何父目录):.git`.@Patrick我没有投票什么? (2认同)
  • 它不起作用,正如人们在之前的评论中已经说过的那样 (2认同)

alb*_*o56 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
Run Code Online (Sandbox Code Playgroud)


pen*_*han 9

我试过很多次了!我终于明白了!

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

请记住,请不要添加.git给你

-C dir


小智 6

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

git --git-dir=<PATH-TO-REPO>/.git --work-tree=<PATH-TO-REPO> <git-command>
Run Code Online (Sandbox Code Playgroud)

例如,如果你想执行 git status:

 git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  status
Run Code Online (Sandbox Code Playgroud)

或者如果您想检查存储库所在的分支:

git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  rev-parse --abbrev-ref HEAD
Run Code Online (Sandbox Code Playgroud)