Skip to content

Instantly share code, notes, and snippets.

@Mark-Booth
Forked from lth2h/git-branch-status
Last active October 21, 2024 13:04
Show Gist options
  • Save Mark-Booth/5058384 to your computer and use it in GitHub Desktop.
Save Mark-Booth/5058384 to your computer and use it in GitHub Desktop.
Version of git-branch-status which only shows the current branch and only generates output if a branch is ahead or behind.Added options to:* Show all branches (revert to the old behaviour)* Show output even if the branch isn't ahead or behind (revert to the old behaviour)* Show branch(es) with respect to origin/master (inspired by git-branches-v…
#!/bin/bash
# forked from https://gist.github.com/jehiah/1288596 @ e357c1e by lth2h
# forked from https://gist.github.com/lth2h/4177524 @ ae184f1 by mark.booth
# this prints out some branch status
# (similar to the '... ahead' info you get from git status)
# example:
# $ git branch-status -a
# dns_check (ahead 1) | (behind 112) origin/master
# master (ahead 2) | (behind 0) origin/master
# $ git branch-status
# master (ahead 2) | (behind 0) origin/master
usage="$(basename "$0") [-hav] -- summarise status of branch(es)
where:
-h show this help text
-a show all branches, not just the current one
-v verbose, show output even if counts are zero"
while getopts 'hav' option; do
case "$option" in
h) echo "$usage"
exit
;;
a) filter=refs/heads
;;
v) verbose=true
;;
?) printf "illegal option: '%s'\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z $filter ] ; then
filter=$(git symbolic-ref -q HEAD)
fi
#echo $# : $* : $filter : $quiet
git for-each-ref --format="%(refname:short) %(upstream:short)" $filter | \
while read local remote
do
[ -z "$remote" ] && continue
DELTAS=$(git rev-list --left-right ${local}...${remote})
LEFT_AHEAD=$(echo "$DELTAS" | grep -c '^<')
RIGHT_AHEAD=$(echo "$DELTAS" | grep -c '^>')
if [ $verbose ] || [ $LEFT_AHEAD -gt 0 ] || [ $RIGHT_AHEAD -gt 0 ] ; then
echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
fi
done
@vbjay
Copy link

vbjay commented Jun 4, 2013

It would be great to have another parameter that allows a different remote to be used to compare. Example:

git branch-status -av upstream

@Mark-Booth
Copy link
Author

@vbjay - I've added a -u option (amongst other changes).

@kierun
Copy link

kierun commented Jul 27, 2015

Thanks Mark, you saved me about a day's work with that! ^_~

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