Skip to content

Instantly share code, notes, and snippets.

@krisr
Created March 23, 2011 22:02
Show Gist options
  • Save krisr/884124 to your computer and use it in GitHub Desktop.
Save krisr/884124 to your computer and use it in GitHub Desktop.
git branches - lists local and remote branches sorted by date
#!/usr/bin/env bash
#
# Prints local branches and remote branches prefixed with your git email
# username sorted by date.
set -o errexit
set -o nounset
USER_NAME=`git config user.email | sed s:\@.*::`
TAN="\033[33m"
NO_COLOR="\033[0m"
# First argument will be printed to the header
# Second argument is assumed to be the prefix of the refs to print
# Last argument is the part of the prefix that will be stripped from the output
function listBranches {
echo -e "$TAN""$1:$NO_COLOR"
git for-each-ref --sort=-committerdate \
--format='date="%(committerdate)"; name="%(refname)"' $2 | sed s:$3:: | \
while read entry
do
# Eval the line git printed out to read the variables into the environment
# variables so we can use printf to format better.
eval "$entry"
# Print out the name, followed by the appropriate number of spaces to
# put the date right aligned.
printf "%s %*s\n" $name "$(( $(tput cols) - ${#name} - 1 ))" "$date"
done
}
listBranches "Local branches" refs/heads/ refs/heads/
echo ""
listBranches "Remote branches for $USER_NAME" refs/remotes/origin/$USER_NAME* refs/remotes/origin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment