List Git branches and their remote tracking branches side by side

I had a hard time following my own branches in a project. They got pretty numerous, and I wasn’t sure if I pushed them to origin at all. git branch -a can list all the branches, including remote ones, but, as my list grew too big, it was impossible to follow it any more.

Thus, I have created a small script called git-branches-with-remotes, which does the work for me. Its only requirements are git (of course), and the column command, which is pretty obviously present on every POSIX compliant systems (even OSX).

#! /bin/sh

COLUMN=`which column 2> /dev/null`
if test -z $COLUMN
then
    echo "\`column' is not found in PATH. Cannot continue."
    exit 1
fi

current_branch=`git rev-parse --abbrev-ref HEAD`

for branch in $(git for-each-ref --shell --format='%(refname)' refs/heads | sed -e s/^\'refs\\/heads\\/// -e s/\'$//)
do
    remote=`git config branch.$branch.remote`
    merge=`git config branch.$branch.merge | sed -e 's/^refs\/heads\///'`

    [ x"$current_branch" == x"$branch" ] && echo -n '*'

    echo -n "$branch"

    if ! test -z $merge
    then
        echo -en "\t"
        echo -n $remote
        echo -n /
        echo -n $merge
    fi

    echo
done | $COLUMN -t

I just put it in my path, and git branches-with-remotes does the work!

Edit (16 August): I have added some code to mark the current branch (if any) with an asterisk. Also, I have put this script in a gist.

Edit (26 February, 2015): It turns out that git branch -vv shows the same information and some more: it also shows if the branches are diverged, and the first line of the last commit’s message.

contacts & more