Skip to content

Instantly share code, notes, and snippets.

@odracci
Forked from vitalk/git-branch-status
Last active August 29, 2015 14:06
Show Gist options
  • Save odracci/197935d19589d0c79cda to your computer and use it in GitHub Desktop.
Save odracci/197935d19589d0c79cda to your computer and use it in GitHub Desktop.
#!/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