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

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

Git pulling depends on the current dir

gitgit-pull

提问by ДМИТРИЙ МАЛИКОВ

I am trying to git pullsome repository via root user from any directory.

我正在尝试git pull通过 root 用户从任何目录访问某个存储库。

For example, executing git pullfrom /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 pullfrom /:

并执行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 pulling command?

为什么当前目录会影响git pulling 命令?

How can that redundant cdbe avoided?

如何cd避免这种冗余?

采纳答案by amcnabb

In your first example, the git command runs as user dmalikovwith the current directory /root. Since the git pullcommand is equivalent to a git fetchfollowed by a git merge, and since git mergeoperates 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 pullwill fail because it can't find the working tree.

甚至你的第二个例子也不能像你期望的那样工作。如果要拉取实际更改(而不是“已经是最新的”),那么git pull它将失败,因为它找不到工作树。

You have a few simple options:

您有几个简单的选择:

1) You can just do the git fetchportion 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 cdto 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。/ 作为当前目录显然有效,因为每个人都有权更改到该目录。