Skip to content

Instantly share code, notes, and snippets.

@ianlintner-wf
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save ianlintner-wf/9956849 to your computer and use it in GitHub Desktop.

Select an option

Save ianlintner-wf/9956849 to your computer and use it in GitHub Desktop.
Poll the remote git repository using git fetch & then git status && pull from git hub based on the status of the branch.
#!/bin/bash
#Current (Remote) Branch
currentbranch=""
#Fetch from the remote branch
echo "polling..."
if [ -n "$currentbranch" ]; then
git fetch $currentbranch
else
git fetch
fi
#Poll the status of the repo
status=$(git status -uno)
#Regex to search the status message e.g. branch is behind
regex="Your branch is behind"
#Run the regex match
if [[ $status =~ $regex ]] ; then
echo "Your branch is behind.\nupdating..."
#Pull from git repo
if [ -n "$currentbranch" ]; then
git pull $currentbranch
else
git pull
fi
#optional for use with submodules
git submodule update --init
#Insert code here to perform other functions following pull.
else
#Branch is up to date
echo "Your branch is up to date."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment