15

pwd is "present working directory". Here's the situation.

pwd:            /path/to/pwd/
git repository: /repo/path/.git/

I want to do a git pull from origin, but without changing my current directory.

To clarify just a little more in case I'm not clear enough, this is the result I want, but I want to do it with one command instead of having to change directories:

$ cd /repo/path
$ git pull origin master
$ cd -
Matthew
  • 6,013
  • 8
  • 38
  • 53
  • What's the use case for this? (I'm just curious.) – KajMagnus Apr 08 '17 at 05:46
  • @KajMagnus This was over 5 years ago, I honestly can't remember. Probably something like wanting to update the repo with a one-liner so I could use `^r` to run it repeatedly as needed, and without messing up the directory stack because I frequently use `pushd`, `popd`, and `cd -`. – Matthew Apr 13 '17 at 22:43
  • 1
    Possible duplicate of [git --git-dir not working as expected](http://stackoverflow.com/questions/1386291/git-git-dir-not-working-as-expected) – pjgranahan Apr 27 '17 at 07:11

2 Answers2

23
git --work-tree=/repo/path --git-dir=/repo/path/.git pull origin master
Justin ᚅᚔᚈᚄᚒᚔ
  • 14,744
  • 6
  • 50
  • 63
  • This is pretty difficult to apply as it stands. Can you provide an example with a public repo? – Cognitiaclaeves Sep 06 '16 at 19:55
  • @Cognitiaclaeves, not sure what you mean by "difficult to apply"? Just tried it and it "just worked". It should work with any repo you have access too public or not. – Andrew Savinykh Mar 05 '17 at 20:43
4

bash -c "cd /repo/path; git pull origin master"

sethcall
  • 2,737
  • 1
  • 17
  • 22
  • Is there really no git option for this? Git is exploding with functionality, it seems hard to believe you MUST have your working directory in repository location or subdirectory of it. – Matthew Mar 12 '12 at 19:06
  • Oh, I have no idea. If there is a way I'm all ears! :) I just wanted to give you a one-liner. – sethcall Mar 12 '12 at 19:08
  • 1
    no need for `bash -c`, you can use `(list)` to execute list in a subshell: `(cd /repo/path; git pull origin master)` – Micha Wiedenmann Apr 27 '17 at 09:37