2

This is for a shell script that will periodically check github for the latest code, and download, compile, and run it if there is any difference between what I already have locally and what was pulled.

I know I can always clone it to a new directory, and then check for changes, and go from there, but that seems incredibly wasteful, as git should already have the logic for checking for differences between files and updating only those.

To complicate the matter, the "local" version may at some point be the "latest" version, and in this case I want to discard the "new" changes and pull from the "stable" repository

I currently have this:

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

but it fails when the "local" is "newer" - suggestions?

update: I have looked into the clean option, however that gets rid of files excluded in the .gitignore - I want a solution that leave those alone, but discards any changes to the source files and pulls the latest from github.

update #2: I think this is what I need:

git fetch --all
git reset --hard origin/master

but I will need to do some testing, some confirmation that it does what I am looking for would be nice

update #3: it seems either

git reset --hard origin/master
git reset --hard HEAD

works as far as overwriting the local files, but now how do I know when the two are different (looking into git diff.. )

4
  • stackoverflow.com/a/19442789/6309 could help regarding the clean option.
    – VonC
    Commented Jun 23, 2014 at 20:25
  • I am not looking to clean untracked files, I am looking to overwrite the local tracked files with the versions from the repository, regardless of if the local is older or newer Commented Jun 24, 2014 at 13:50
  • What error message do you have when local is newer? the reset --hard should work in every case. It is what I have proposed in the answer.
    – VonC
    Commented Jun 24, 2014 at 13:53
  • No error message, but it doesn't run - If I were to edit a local file, then run the git pull, it says that it's already up to date and does not enter the if, however I want it to recognize that it's different from the remote, and have it re-set to the remote version. Commented Jun 24, 2014 at 14:51

2 Answers 2

2

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.

2
  • I need this to run periodically to remove any potential local changes anyways, so listening for changes wouldn't solve the problem. Thanks for the -C option though, that does make it a bit cleaner. Commented Jun 20, 2014 at 17:04
  • 1
    @user2813274 ok, I just wanted to mention the other option "notification", as opposed to "polling".
    – VonC
    Commented Jun 20, 2014 at 18:00
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.