Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pulling depends on the current dir

Tags:

git

git-pull

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 pulling command?

How can that redundant cd be avoided?

like image 660
ДМИТРИЙ МАЛИКОВ Avatar asked Mar 24 '12 12:03

ДМИТРИЙ МАЛИКОВ


People also ask

Does it matter where you git pull?

Not necessarily. Any local commits you have on the branch you're pulling will be merged with the changes upstream.

How does pull work in git?

How Does It Work? Git pull, in a nutshell, is a two-part process. First, your remote-tracking branch is synced with the “true” branch in the remote repository. Then, your local branch is compared to the remote-tracking branch and receives the new commits so it can catch up to the current state of the remote branch.

Does git pull affect working directory?

Yes, as the pull docs say: git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch. With --rebase, it runs git rebase instead of git merge. A merge or rebase will affect your working tree.

How does git determine pull changes?

git fetch git log --name-status origin/master.. Will show you what commits you are about to retrieve, along with the names of the files. Based upon this reply the command "git log --graph -p" is doing a nice job. It shows tree information about the history and code changes as well.


1 Answers

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)
like image 83
amcnabb Avatar answered Sep 28 '22 13:09

amcnabb