Skip to content

Instantly share code, notes, and snippets.

@knovoselic
Forked from jehiah/git-branch-status
Last active August 29, 2015 14:07
Show Gist options
  • Save knovoselic/1a20dc28dbb0141fc176 to your computer and use it in GitHub Desktop.
Save knovoselic/1a20dc28dbb0141fc176 to your computer and use it in GitHub Desktop.
#!/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
TOTAL_DIFFERENCES=0
CGREEN='\033[1;32m'
CYELLOW='\033[1;33m'
CRED='\033[1;31m'
CEND='\033[0m'
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)
TOTAL_DIFFERENCES=$(($LEFT_AHEAD + $RIGHT_AHEAD + $TOTAL_DIFFERENCES))
MSG_LEFT_AHEAD="(ahead $LEFT_AHEAD)"
MSG_RIGHT_AHEAD="(behind $RIGHT_AHEAD)"
if [ "$LEFT_AHEAD" -ne 0 ]; then
MSG_LEFT_AHEAD="$CYELLOW$MSG_LEFT_AHEAD$CEND"
fi
if [ "$RIGHT_AHEAD" -ne 0 ]; then
MSG_RIGHT_AHEAD="$CRED$MSG_RIGHT_AHEAD$CEND"
fi
echo -e "$local $MSG_LEFT_AHEAD | $MSG_RIGHT_AHEAD $remote"
done < <(git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads)
if [ "$TOTAL_DIFFERENCES" == 0 ]; then
echo -e "${CGREEN}Everything is synchronized.$CEND"
fi
@bill-auger
Copy link

gj bud :)

building on this version - i prettified it a bit more:

  • formatted results into columns
  • added ability to filter by branch name and to show locals
  • removed the temp file i/o
  • added some switches and usage message

https://gist.github.com/bill-auger/9335fe2633eae38d3070

@knovoselic
Copy link
Author

@bill-auger Great job, thanks for sharing!

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