Skip to content

Instantly share code, notes, and snippets.

@schlomok
Last active November 8, 2021 20:40
Show Gist options
  • Save schlomok/2529cce16cd982293fff to your computer and use it in GitHub Desktop.
Save schlomok/2529cce16cd982293fff to your computer and use it in GitHub Desktop.
Checks if current git branch needs to pull, push, if it has diverged, or if it is up to date with the remote branch.
#!/bin/sh
# This script checks if the current branch needs to pull, push, if it has
# diverged, or if it is up-to-date with the remote branch.
# Credits to: http://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
# Check if git is installed.
hash git 2>/dev/null || { echo >&2 "This script requires git to be installed."; exit 1; }
# Update remote branch to fetch latest remote commit SHA.
git remote update
# Grab all sha's.
local_commit=$(git rev-parse @)
remote_commit=$(git rev-parse @{u})
base_commit=$(git merge-base @ @{u})
# Check branch status
if [ $local_commit = $remote_commit ]; then
echo "Branch is up-to-date with origin."
elif [ $local_commit = $base_commit ]; then
echo "Branch is behind origin."
elif [ $remote_commit = $base_commit ]; then
echo "Branch is ahead of origin."
else
echo "Branch has diverged."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment