我正在尝试从一个不同的目录比我在运行git。例如,如果我在:

cd /home/domain/
git status << runs perfect ie
# On branch master
# Your branch is ahead of 'origin/master' by 6 commits.

所以现在我想使用——git-dir选项从另一个目录运行这个命令。

假设我在root目录下,然后试试这个:

git --git-dir="/home/domain/" status
## Error 
fatal: Not a git repository: '/home/domain/'

我还尝试了包含。git文件夹。

git --git-dir="/home/domain/.git/" status

但这看起来像是试图从根目录运行git,即删除域文件夹中的所有内容,并在根目录中添加所有内容。

希望有人能告诉我我做错了什么。


您还必须定义工作目录。我知道这很令人困惑,但这是一个灵活性的问题。

git --git-dir=/mycode/.git --work-tree=/mycode status

你可以在这里读到更多


根据你上面的评论,听起来你仍然遇到了一个问题:

root@erx [/]# git --git-dir=/home/domain/.git --work-tree=/home/domain/ pull origin master
fatal: /usr/local/libexec/git-core/git-pull cannot be used without a working tree

听起来您可能打算从crontab或其他地方运行此程序。最好先使用cd切换到工作目录。例如:

root@erx [/]# (cd /home/domain && git pull origin master)

这将临时(在一个子shell中,这就是括号所做的)将当前目录更改为/home/domain,然后运行git pull origin master。命令完成后,您的当前目录仍然是命令之前的目录。


从git 1.8.5开始(应该会在下周发布),它将变得更加简单:

 git -C "/home/domain/" status

不再需要设置——git-dir和——work-tree !

然而,正如OmarL在评论中指出的那样:

我发现-C并不完全等同于——git-dir——work-tree,因为-C并不覆盖GIT_DIR环境变量。


参见Nazri Ramliy的commit 44e1e4:

It takes more keypresses to invoke git command in a different directory without leaving the current directory: (cd ~/foo && git status) git --git-dir=~/foo/.git --work-tree=~/foo status GIT_DIR=~/foo/.git GIT_WORK_TREE=~/foo git status (cd ../..; git grep foo) for d in d1 d2 d3; do (cd $d && git svn rebase); done The methods shown above are acceptable for scripting but are too cumbersome for quick command line invocations. With this new option, the above can be done with fewer keystrokes: git -C ~/foo status git -C ../.. grep foo for d in d1 d2 d3; do git -C $d svn rebase; done


git --git-dir="/home/domain/" status
## Error 
fatal: Not a git repository: '/home/domain/'

在Git 2.26 (Q1 2020)中,文档更加清晰。

指定GIT_DIR的位置(使用环境变量,或者使用"git——git-dir=<where> cmd"选项)的一个效果是禁用存储库发现。

这一点在文档中被强调了一些,因为新用户经常会感到困惑。

参见HebaWaly提交d82ad54(2020年1月30日)。 (由Junio C Hamano—gitster—在commit 17e4a1b中合并,2020年2月12日)

git: update documentation for --git-dir Signed-off-by: Heba Waly Helped-by: Junio C Hamano git --git-dir <path> is a bit confusing and sometimes doesn't work as the user would expect it to. For example, if the user runs git --git-dir=<path> status, git will skip the repository discovery algorithm and will assign the work tree to the user's current work directory unless otherwise specified. When this assignment is wrong, the output will not match the user's expectations. This patch updates the documentation to make it clearer.

所以git——git-dir的文档现在包括:

--git-dir=<path>: Set the path to the repository (".git" directory). This can also be controlled by setting the GIT_DIR environment variable. It can be an absolute path or relative path to current working directory. Specifying the location of the ".git" directory using this option (or GIT_DIR environment variable) turns off the repository discovery that tries to find a directory with ".git" subdirectory (which is how the repository and the top-level of the working tree are discovered), and tells Git that you are at the top level of the working tree. If you are not at the top-level directory of the working tree, you should tell Git where the top-level of the working tree is, with the --work-tree=<path> option (or GIT_WORK_TREE environment variable)