git pull for a local repository at specified location (instead of pwd)
Asked Answered
E

3

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 -
Eckhart answered 12/3, 2012 at 19:1 Comment(3)
What's the use case for this? (I'm just curious.)Postnatal
@Postnatal 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 -.Eckhart
Possible duplicate of git --git-dir not working as expectedExtenuate
E
25
git --work-tree=/repo/path --git-dir=/repo/path/.git pull origin master
Enrika answered 12/3, 2012 at 19:6 Comment(4)
This is pretty difficult to apply as it stands. Can you provide an example with a public repo?Palatalized
@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.Tutti
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/pathBountiful
@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.Homegrown
B
4

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

Bridgeman answered 12/3, 2012 at 19:3 Comment(3)
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.Eckhart
Oh, I have no idea. If there is a way I'm all ears! :) I just wanted to give you a one-liner.Bridgeman
no need for bash -c, you can use (list) to execute list in a subshell: (cd /repo/path; git pull origin master)Plausible
C
1

The -C [/repo/path/] seems to work for me.

git -C /repo/path/ pull

Of course replacing the /repo/path/... with your actual path. This should work for any git command, like you might want to preface your git pull command with:

git -C /repo/path/ status so you can confirm you are on the right branch

Claudette answered 23/7 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.