Skip to content
Advertisement

Script for root to git pull as another user

I have a script that I would like to have do a git pull inside another user’s git directory. This script is run by the root user. For example:

cd /home/username/GitProject
sudo -u username -i git pull

When I run this, I get:

fatal: Not a git repository (or any of the parent directories): .git

Is there a way to have my script do a git pull as username?

Advertisement

Answer

Try without the -i option to sudo. That option is documented as first changing to the target user’s home directory, which undoes the directory change you so carefully do before that. Alternatively, use the appropriate options to git to specify the directory, something like this:

sudo -u username -i git --git-dir=/home/username/GitProject/.git --work-tree=/home/username/GitProject pull
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement