Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Auto update local repository with remote github repository.
#!/usr/bin/env python3
Author: Ming Wen (
This script is used to track the changes in the github, and if a new commit
is found, update the local repository accordingly.
from urllib.request import urlopen
from http.client import HTTPResponse
from subprocess import check_output
import json
import os
import sys
# Configurations
REPO = ""
BRANCH = "master"
def github_sync(directory):
remote_sha = fetch_remove_sha()
local_sha = fetch_local_sha()
if remote_sha != local_sha:
check_output(["git", "pull", "origin", BRANCH])
print("The local repo has been updated")
return 1;
print("The local repo is already up-to-date")
return 0;
def fetch_remove_sha():
req_url = "" + \
USERNAME + "/" + REPO + "/branches/" + BRANCH
resp = urlopen(req_url)
resp_str = str(, encoding="utf-8")
resp_data = json.loads(resp_str);
remote_sha = resp_data["commit"]["sha"]
return remote_sha
def fetch_local_sha():
check_output(["git", "checkout", BRANCH])
local_sha = str(check_output(["git", "rev-parse", "HEAD"]), encoding="utf-8")
return local_sha[:-1] # remove newline
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment