How to keep a folder up-to-date with a git repo?

First, since git 1.8.5 (Nov 2013), you can use the -C option for git, as in "git --git-dir not working as expected".

status=`git -C $gitDir pull`
if [ "$status" != "Already up-to-date." ];
then
git -C $gitDir fetch origin
git -C $gitDir reset --hard origin/master

Second, you could listen on your client for a JSON payload coming from GitHub webhook triggered on a push to that GitHub repo.
That way, you wouldn't have to execute your script unless it is actually needed.


I wrote about this recently here: https://www.debugandrelease.com/how-to-automatically-keep-git-repositories-up-to-date/

My solution was in Powershell where the script iterates over folders, determines if there is a .git subfolder, calls git stash, git pull and then git stash pop. All of this functionality is available by installing the package and then running the command.

$ Install-Module -Name UpdateGitRepositories
$ Update-GitRepositories