git --git-dir 没有按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1386291/
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
git --git-dir not working as expected
提问by Lee
I am trying to run git from a different directory than I am in. So for example if I am in:
我正在尝试从与我所在的目录不同的目录运行 git。例如,如果我在:
cd /home/domain/
git status << runs perfect ie
# On branch master
# Your branch is ahead of 'origin/master' by 6 commits.
So now I want to run this command from a different directory using the --git-dir
option.
所以现在我想使用该--git-dir
选项从不同的目录运行此命令。
So lets say I'm in root/
and try this:
所以让我们说我在root/
试试这个:
git --git-dir="/home/domain/" status
## Error
fatal: Not a git repository: '/home/domain/'
I've also tried to include the .git
folder i.e.
我也尝试包含.git
文件夹,即
git --git-dir="/home/domain/.git/" status
But this looks like it's trying to run git from the root, i.e. deleting everything from my domain folder and adding everything in root.
但这看起来像是试图从根目录运行 git,即从我的域文件夹中删除所有内容并在根目录中添加所有内容。
Hope someone can advise on what I'm doing wrong.
希望有人能就我做错了什么提出建议。
回答by Jon Gretar
回答by VonC
Starting git 1.8.5(which should be out next week), it will be even simpler:
从git 1.8.5(下周应该会发布)开始,它会更简单:
git -C "/home/domain/" status
No need to set --git-dir
and --work-tree
anymore!
无需设置--git-dir
和--work-tree
了!
See commit 44e1e4by Nazri Ramliy:
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-dir=~/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 命令需要更多的按键:
(cd ~/foo && git status)
git --git-dir=~/foo/.git --work-dir=~/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
上面显示的方法对于脚本编写是可以接受的,但是对于快速命令行调用来说太麻烦了。
使用这个新选项,可以通过更少的击键完成上述操作:
git -C ~/foo status
git -C ../.. grep foo
for d in d1 d2 d3; do git -C $d svn rebase; done
回答by Greg Hewgill
Based on your comment above, it sounds like you are still running into a problem:
根据您上面的评论,听起来您仍然遇到问题:
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
It sounds like you might be intending to run this from crontab
or something. You may be better off using cd
to switch to your working directory first. For example:
听起来你可能打算运行它crontab
或其他东西。您最好先使用cd
切换到您的工作目录。例如:
root@erx [/]# (cd /home/domain && git pull origin master)
This will temporarily (in a subshell, which is what the parentheses do) change the current directory to /home/domain
, and then run git pull origin master
. After the command is complete, your current directory remains whatever it was before the command.
这将暂时(在子shell 中,这就是括号的作用)将当前目录更改为/home/domain
,然后运行git pull origin master
. 命令完成后,您的当前目录保持在命令之前的任何位置。
回答by VonC
git --git-dir="/home/domain/" status
## Error
fatal: Not a git repository: '/home/domain/'
With Git 2.26 (Q1 2020), the documentation is clearer.
使用 Git 2.26(2020 年第一季度),文档更加清晰。
One effect of specifying where the GIT_DIR
is (either with the environment variable, or with the "git --git-dir=<where> cmd
" option) is to disable the repository discovery.
指定位置GIT_DIR
(使用环境变量或使用“ git --git-dir=<where> cmd
”选项)的一种效果是禁用存储库发现。
This has been placed a bit more stress in the documentation, as new users often get confused.
由于新用户经常感到困惑,因此在文档中增加了一些压力。
See commit d82ad54(30 Jan 2020) by Heba Waly (HebaWaly
).
(Merged by Junio C Hamano -- gitster
--in commit 17e4a1b, 12 Feb 2020)
请参阅Heba Waly ( ) 的commit d82ad54(30 Jan 2020 )。(由Junio C Hamano合并-- --在commit 17e4a1b,2020 年 2 月 12 日)HebaWaly
gitster
git
: update documentation for --git-dirSigned-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 的文档签字人:Heba Waly
帮助人:Junio C Hamano
git --git-dir <path>
有点混乱,有时不像用户期望的那样工作。例如,如果用户运行
git --git-dir=<path> status
,除非另有说明,否则 git 将跳过存储库发现算法并将工作树分配到用户的当前工作目录。
当这个分配错误时,输出将不符合用户的期望。此补丁更新了文档以使其更加清晰。
So the documentation for git --git-dir
now includes:
所以现在的文档git --git-dir
包括:
--git-dir=<path>:
Set the path to the repository ("
.git
" directory).
This can also be controlled by setting theGIT_DIR
environment variable.
It can be an absolute path or relative path to current working directory.Specifying the location of the "
.git
" directoryusing this option (orGIT_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 (orGIT_WORK_TREE
environment variable)
--git-dir=<path>:
设置存储库的路径(“
.git
”目录)。
这也可以通过设置GIT_DIR
环境变量来控制。
它可以是当前工作目录的绝对路径或相对路径。
.git
使用此选项(或GIT_DIR
环境变量)指定“ ”目录的位置会关闭尝试查找带有“.git
”子目录的目录的存储库发现(这是发现存储库和工作树顶级的方式),并告诉 Git 您位于工作树的顶层。如果你不在工作树的顶级目录,你应该告诉 Git 工作树的顶级在哪里,用
--work-tree=<path>
选项(或GIT_WORK_TREE
环境变量)