Skip to content

Instantly share code, notes, and snippets.

@tueda
Created December 26, 2024 03:45
Show Gist options
  • Save tueda/ce103cd8f5288c1bd1b4ce37e56ef84c to your computer and use it in GitHub Desktop.
Save tueda/ce103cd8f5288c1bd1b4ce37e56ef84c to your computer and use it in GitHub Desktop.
List branches sorted by commit dates. #bin #bash #git
#!/bin/bash
#
# List branches sorted by commit dates.
#
# Usage:
# git latest [--all|--tags]
#
set -eu
set -o pipefail
refs=refs/heads
while (( $# > 0 )); do
case "$1" in
--all)
refs=refs/
shift
;;
--tags)
refs=refs/tags/
shift
;;
*)
echo "error: illegal option: $1" >&2
exit 1
;;
esac
done
maxlen() {
awk '{ if (length > max) max = length } END { print max }'
}
refname_len=$(git for-each-ref --format='%(refname:short)' $refs | maxlen)
git for-each-ref --sort=-committerdate --color=always \
--format="%(HEAD) %(color:yellow)%(align:$refname_len)%(refname:short)%(end)%(color:reset) %(color:magenta)[%(committerdate:short)]%(color:reset) %(contents:subject) %(color:cyan)@%(authorname)%(color:reset)" \
$refs | less -R -S -F -X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment