0

I`m writing a Powershell script that (among other things) fetches some data from a Git repository. Normally, I would do something like this:

# Verify that the repo exists, otherwise create it
if(!(Test-Path $local-repo-path)){
    git clone $repo-url $local-repo-path
}

cd $local-repo-path
git pull
cd "some-inner-directory\..."
# do various tasks here...
cd ..
# Perform other tasks...

As you can see, I'm navigating into the folder structure in the repo here in order to perform a set of tasks. It seems to me that it would be a lot cleaner to stay outside of the repo in Powershell, and pass the necessary paths in as parameters when needed. In other words, I'd like to avoid using cd <somepath>, and cd ...

I can do that for most operations, but not for git pull.

My question: Is there any way to perform a git pull from outside of the repository directory? I.e., something like the following (this is invalid syntax):

git pull $local-repo-path
Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • why not create an alias for the path, that way it will still remain short? – Michael Sep 24 '19 at 12:04
  • check for git qualifier: `--git-dir=` and `--work-tree=`. – Serge Sep 24 '19 at 12:08
  • @Serge Thanks, but apparently there is an even easier method, as in the linked-to answer. I'm surprised I didn't find it earlier on my own. **Short answer:** `git -C some/path/ pull`. That will work for pretty much any other git command too - it tells Git where to perform it, which was exactly what I was looking for. – Kjartan Sep 24 '19 at 12:32

0 Answers0