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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7149984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:39:45  来源:igfitidea点击:

How do I execute a Git command without being in the repository?

git

提问by Tower

Is there a way to execute Git commands against a repository without being in that repository?

有没有办法对存储库执行 Git 命令而不在该存储库中?

For example something like this: git /home/repo log?

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

Please do not tell me to cdto it. I'm doing this via an execcall.

请不要告诉我cd要它。我是通过exec电话来做这件事的。

采纳答案by max

Try:

尝试:

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

It is important to give the path all the way up to the .git directory of your repository. Otherwise you will get only an error message that says something like:

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

fatal: Not a git repository

回答by calandoa

Use -Coption (docs):

使用-C选项(文档):

git -C /home/repo log

This is almostequivalent to --git-dirand --work-treewithout appending the usual .gitfolder

几乎等同于--git-dir并且--work-tree不附加通常的.git文件夹

Edit:

编辑:

Answer downvoted... quite amazing, as, strictly speaking, this is the only correct answer to the question. The options --git-dirand --work-treeare not existing to access the repository from outside the work tree, they are used to move the .gitsomewhere else, and they are much more complicated to use in some cases.

答案被否决......非常惊人,因为严格来说,这是该问题的唯一正确答案。从工作树外部访问存储库的选项--git-dir--work-tree不存在,它们用于将存储库移动到.git其他地方,并且在某些情况下使用起来要复杂得多。

For instance, to get the log of /home/repo/subdironly:

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

git -C /home/repo/subdir log .

or

或者

git -C /home/repo log subdir

It is not possible to use log .with --git-diror --work-tree. The path must be processed to extract the subpath relative to the top of the work tree, and even in that case, git will not recognize it as a path if you do not use the --option, so the only possible way is:

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

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

Furthermore, --work-treeis not working at all with logsubcommand with my version (git 1.9.1). It is just ignored:

此外,对于我的版本(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

I do not even understand if this is a bug or a feature... as usual with many git design choices.

我什至不明白这是一个错误还是一个功能......像往常一样有许多 git 设计选择。

回答by alberto56

In fact you need to use --git-dir and --work-tree together. Here is an example:

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

回答by penny chan

I have tried many times! I finally got it!

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

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

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

do remember, please don't add .git to your

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

-C dir

-C 目录

回答by Brian Vandenberg

This is similar to @max's answer. Unfortunately, --git-dir didn't do what I needed. editIn retrospect, another [previous] answer I hadn't read suggested using --work-tree. I'm not sure whether using environment or flags is more appropriate so I'll leave my answer in case someone finds use in it, but I'll be switching to use --work-tree/ --git-dir.

这类似于@max 的回答。不幸的是, --git-dir 没有做我需要的。编辑回想起来,我没有读过的另一个 [previous] 答案建议使用--work-tree. 我不确定使用 environment 还是 flags 更合适,所以我会留下我的答案,以防有人发现它有用,但我会改用--work-tree/ --git-dir



There's two repos, one inside the other but not a submodule; the outer repo ignores it:

有两个 repos,一个在另一个内部,但不是子模块;外部回购忽略它:

tools (outer repo)
  gcc/4.9.2 (inner repo)

What I wanted was the output of git rev-parse --show-prefixrelative to the outer repo. Here's what I came up with (bash syntax; split across lines for readability):

我想要的是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)

When executed from within 4.9.2 it produces the string gcc/4.9.2.

从 4.9.2 中执行时,它会生成 string gcc/4.9.2

回答by Paula

For any git command, you can do:

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

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

For example, if you want to do a git status:

例如,如果你想做一个 git status:

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

or if you want to check the branch the repo is in:

或者,如果您想检查回购所在的分支:

git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  rev-parse --abbrev-ref HEAD