Skip to content

Instantly share code, notes, and snippets.

@vitalk
Created January 26, 2014 21:42
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vitalk/8639831 to your computer and use it in GitHub Desktop.
Save vitalk/8639831 to your computer and use it in GitHub Desktop.
Show how many commits each branch is ahead or behind its upstream.
#!/usr/bin/env bash
# Usage: git-branch-status
# Show how many commits each branch is ahead or behind its upstream.
branch=`git rev-parse --abbrev-ref HEAD`
git for-each-ref --format='%(refname:short) %(upstream:short)' refs/heads | \
while read local upstream; do
# Use master if upstream branch is empty
[ -z $upstream ] && upstream=master
ahead=`git rev-list ${upstream}..${local} --count`
behind=`git rev-list ${local}..${upstream} --count`
if [[ $local == $branch ]]; then
asterisk=*
else
asterisk=' '
fi
# Show asterisk before current branch
echo -n "$asterisk $local"
# Does this branch is ahead or behind upstream branch?
if [[ $ahead -ne 0 && $behind -ne 0 ]]; then
echo -n " ($ahead ahead and $behind behind $upstream)"
elif [[ $ahead -ne 0 ]]; then
echo -n " ($ahead ahead $upstream)"
elif [[ $behind -ne 0 ]]; then
echo -n " ($behind behind $upstream)"
fi
# Newline
echo
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment