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,351
  • 8
  • 40
  • 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

24
git --work-tree=/repo/path --git-dir=/repo/path/.git pull origin master
Justin ᚅᚔᚈᚄᚒᚔ
  • 15,081
  • 7
  • 52
  • 64
  • 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
  • Yes as of 2022 you still have to specify the .git location and the work tree path, somewhat as annoying as having to cd in and out. Might as well use `cd /repo/path && git pull && cd /current/path` – Peter Kionga-Kamau Sep 30 '22 at 17:08
  • 1
    @PeterKionga-Kamau If you're doing it that way you could use `pushd /repo/path; git pull; popd`, or the old `cd /repo/path; git pull; cd -` so that you don't have to mess about with remembering or typing your working directory. – Justin ᚅᚔᚈᚄᚒᚔ Nov 09 '22 at 15:37
4

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

sethcall
  • 2,837
  • 1
  • 19
  • 22
  • 1
    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