Git 拉取取决于当前目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9851644/
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 pulling depends on the current dir
提问by ДМИТРИЙ МАЛИКОВ
I am trying to git pull
some repository via root user from any directory.
我正在尝试git pull
通过 root 用户从任何目录访问某个存储库。
For example, executing git pull
from /root/
:
例如,git pull
从执行/root/
:
#> cd ~
#> sudo -u dmalikov git --git-dir=/home/dmalikov/path/to/repo/.git pull
/usr/libexec/git-core/git-sh-setup: line 142: cd: /root/.: Permission denied
Cannot chdir to /root/., the toplevel of the working tree
And executing git pull
from /
:
并执行git pull
来自/
:
#> cd /
#> sudo -u dmalikov git --git-dir=/home/dmalikov/path/to/repo/.git pull
Already up-to-date.
Why did current directory affects git pull
ing command?
为什么当前目录会影响git pull
ing 命令?
How can that redundant cd
be avoided?
如何cd
避免这种冗余?
采纳答案by amcnabb
In your first example, the git command runs as user dmalikov
with the current directory /root
. Since the git pull
command is equivalent to a git fetch
followed by a git merge
, and since git merge
operates on the working tree, git tries to hunt for the working tree. As this user does not have permission to cd /root
, the git command fails.
在您的第一个示例中,git 命令dmalikov
以当前目录的用户身份运行/root
。由于该git pull
命令相当于 agit fetch
后跟 a git merge
,并且由于git merge
对工作树进行操作,因此 git 尝试寻找工作树。由于该用户没有权限cd /root
,git 命令失败。
Even your second example doesn't work as you would expect. If there are actual changes to be pulled (instead of "Already up-to-date"), then the git pull
will fail because it can't find the working tree.
甚至你的第二个例子也不能像你期望的那样工作。如果要拉取实际更改(而不是“已经是最新的”),那么git pull
它将失败,因为它找不到工作树。
You have a few simple options:
您有几个简单的选择:
1) You can just do the git fetch
portion of the operation by doing:
1)您可以git fetch
通过执行以下操作来完成操作的一部分:
sudo -u dmalikov git --git-dir=/home/dmalikov/path/to/repo/.git fetch
which doesn't give any error for me.
这对我来说没有任何错误。
2) You can add a cd
to the working tree:
2)您可以cd
向工作树添加一个:
(cd /home/dmalikov/path/to/repo; sudo -u dmalikov git pull)
回答by bluesman
To answer my own comment, the /root was an interesting error
为了回答我自己的评论,/root 是一个有趣的错误
To have it work with --git-dir you also need to specify a work-tree directory
要让它与 --git-dir 一起使用,您还需要指定一个工作树目录
sudo -u dmalikov git --git-dir=/home/dmalikov/path/to/repo/.git --work-tree=/home/dmalikov/path/to/repo/.git pull
回答by ralphtheninja
I don't think it's possible to avoid that cd as you run git with a user that doesn't have permission to change directory back to the current directory, i.e. /root. / as current directory obviously works since everyone has permissions to change to that directory.
我认为在您使用没有权限将目录更改回当前目录(即 /root)的用户运行 git 时,不可能避免该 cd。/ 作为当前目录显然有效,因为每个人都有权更改到该目录。