Skip to content

Instantly share code, notes, and snippets.

@pavelpy
Created October 1, 2018 13:06
Show Gist options
  • Save pavelpy/48cf4af8533b691cc67104276846129f to your computer and use it in GitHub Desktop.
Save pavelpy/48cf4af8533b691cc67104276846129f to your computer and use it in GitHub Desktop.
Task for cron for check if pull needed in Git
#!/bin/sh
# usage: cron_git_pull.sh [optional_branch_name]
git fetch
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
echo "Need to pull"
git pull
elif [ $REMOTE = $BASE ]; then
echo "Need to push"
else
echo "Diverged"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment