Git pulling depends on the current dir
Solution 1
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.
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.
You have a few simple options:
1) You can just do the git fetch
portion of the operation by doing:
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:
(cd /home/dmalikov/path/to/repo; sudo -u dmalikov git pull)
Solution 2
To answer my own comment, the /root was an interesting error
To have it work with --git-dir you also need to specify a work-tree directory
sudo -u dmalikov git --git-dir=/home/dmalikov/path/to/repo/.git --work-tree=/home/dmalikov/path/to/repo/.git pull
Related videos on Youtube
Comments
-
ДМИТРИЙ МАЛИКОВ almost 2 years
I am trying to
git pull
some repository via root user from any directory.For example, executing
git pull
from/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/
:#> 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?How can that redundant
cd
be avoided?-
bluesman about 12 yearsWhere is /root is it a folder inside your repo? If it is you shouldn't have permission issues. If /root is outside of your git structure, the question that begs to be asked is why is it accessing that folder in the first place?
-
Ethan about 12 yearsWhy are you trying to work as root?
-
ДМИТРИЙ МАЛИКОВ about 12 yearsI am trying to work with root because it is a content of eix-sync.conf.
-
-
ДМИТРИЙ МАЛИКОВ about 12 years
fatal: Could not change back to '/root': Permission denied
-
ralphtheninja about 12 yearsChanged my answer. It has to do with permissions and not --work-tree as I thought initially.
-
ДМИТРИЙ МАЛИКОВ about 12 years
fatal: Could not change back to '/root': Permission denied
-
bluesman about 12 yearsWhat machine are you on? It will use your system (windows or linux) specific paths. The following works on my local machine (git bash on windows)
$ git --git-dir=/c/workspace/changes/.git --work-tree=/c/workspace/changes/ pull Already up-to-date.
-
ДМИТРИЙ МАЛИКОВ about 12 yearsTry to execute your command from dir, where you cannot
cd
via non-root user. -
glerYbo over 11 yearsRelated: git --git-dir not working as expected: stackoverflow.com/questions/1386291/…