Skip to content

Instantly share code, notes, and snippets.

@mhafalir
Created November 19, 2015 13:38
Show Gist options
  • Save mhafalir/28753aa2cbd260f708ff to your computer and use it in GitHub Desktop.
Save mhafalir/28753aa2cbd260f708ff to your computer and use it in GitHub Desktop.
git branch status
#!/bin/bash
# by http://github.com/jehiah
# this prints out some branch status (similar to the '... ahead' info you get from git status)
# example:
# $ git branch-status
# dns_check (ahead 1) | (behind 112) origin/master
# master (ahead 2) | (behind 0) origin/master
COLOR_RED="\033[0;31m"
COLOR_YELLOW="\033[0;33m"
COLOR_GREEN="\033[0;32m"
COLOR_OCHRE="\033[38;5;95m"
COLOR_BLUE="\033[0;35m"
COLOR_WHITE="\033[0;37m"
COLOR_RESET="\033[0m"
git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
while read local remote
do
[ -z "$remote" ] && continue
git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
LEFT_AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
RIGHT_AHEAD=$(grep -c '^>' /tmp/git_upstream_status_delta)
printf "$COLOR_GREEN %-30s" "$local"
if [ "$LEFT_AHEAD" -gt 0 ];then
printf "$COLOR_BLUE %-12s" "(ahead $LEFT_AHEAD)"
else
printf "$COLOR_RESET %-12s" "(ahead $LEFT_AHEAD)"
fi;
if [ "$RIGHT_AHEAD" -gt 0 ];then
printf "$COLOR_RED %-12s $COLOR_RESET $remote\n" "(behind $RIGHT_AHEAD)";
else
printf "$COLOR_RESET %-12s $COLOR_RESET $remote\n" "(behind $RIGHT_AHEAD)";
fi;
done
@vardars
Copy link

vardars commented Nov 19, 2015

+rep

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