11

i would like to run a git pull and specify the directory. some say using --git-dir and --work-tree should work but such as

git --git-dir=/var/www/.git --work-tree=/var/www pull

put this returns a "cannot be used without a working tree". The only time i can get this to work is if i use ".", ".." or any directory above the current directory

this post suggests maybe a bug? git pull while not in a git directory

in the end i need to run a sudo git pull in a single line. i welcome any work arounds. thanks

Community
  • 1
  • 1
jadent
  • 3,674
  • 5
  • 27
  • 32

4 Answers4

21

In the two years since this question was answered, Git 1.8 came out which supports calling git in other directories using the -C flag:

git -C /var/www pull
dotancohen
  • 30,064
  • 36
  • 138
  • 197
10

Use sudo and give the command as an argument to sh?

Something like:

sudo sh -c 'cd /dir && git pull'
araqnid
  • 127,052
  • 24
  • 157
  • 134
  • perfect! thanks so much. combining w/ -i switch and sourcing the .bashrc file in .bash_profile i can even have a password on the private key. full command is [code](sudo -i -u www-data sh -c 'cd /var/www && git pull') – jadent Feb 06 '12 at 23:06
  • you can also wrap it with pushd & popd – the.malkolm Feb 07 '12 at 00:14
0

On Windows in MINGW64:

git -C "C:\Trunk190117\crewcontrol-source" pull

or

git -C /c/Trunk190117/crewcontrol-source pull
andrew
  • 3,083
  • 4
  • 24
  • 29
0

You are using --work-dir, which is not the right paramter. It is --work-tree and should work for what you want. Or you can just do cd /var/www && git pull if you wanted the pull to be in a single command / line.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • yeah sorry i am using the --work-tree param when i get that error (didn't copy the command in). ex: [code](git --git-dir=/var/www/.git --work-dir=/var/www pull). Other posts i have found seem to have the same issue. With no --work-tree specified it uses my home directory. I can specify --work-tree as ".", "..", "../.." (which is the / dir) w/o a problem but as soon as i go anywhere else it throws "cannot be used without a working tree". even using "../../var/www" will throw the error – jadent Feb 06 '12 at 16:27