Skip to content

Instantly share code, notes, and snippets.

Created September 29, 2017 05:08
Show Gist options
  • Save anonymous/b5eafa7ed94350f2d74fbc4795d5b127 to your computer and use it in GitHub Desktop.
Save anonymous/b5eafa7ed94350f2d74fbc4795d5b127 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Returns true if git needs to be updated; false otherwise
function update_git() {
pushd "$1"
git remote update
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"
res=0
elif [ "$LOCAL" = "$BASE" ]; then
echo "Need to pull"
res=1
elif [ "$REMOTE" = "$BASE" ]; then
git reset --hard HEAD
git clean -f -d
res=1
else
git reset --hard HEAD
git clean -f -d
res=1
fi
popd
return "$res"
}
@AlecTaylor
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment