Skip to content

Instantly share code, notes, and snippets.

@smuuf
Last active May 28, 2026 14:23
Show Gist options
  • Select an option

  • Save smuuf/fbbaed181e25593532cf57ad1b95b97d to your computer and use it in GitHub Desktop.

Select an option

Save smuuf/fbbaed181e25593532cf57ad1b95b97d to your computer and use it in GitHub Desktop.
git recent-branches alias - list local branches with recent commits
#!/bin/sh
# Install with:
# curl -fsSL https://gist.githubusercontent.com/smuuf/fbbaed181e25593532cf57ad1b95b97d/raw/recent-branches.sh | sh
ALIAS='!r() {
refbranch=${1:-origin/main}; count=${2:-20};
git for-each-ref --sort=-committerdate refs/heads --count=${count} --color \
--format="%(refname:short)|%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)|%(subject)|%(color:magenta)%(authorname)|%(color:reset)" \
| while IFS="|" read -r branch rest; do
branch=$(echo "$branch" | tr -d "* ");
ahead=$(git rev-list --count "${refbranch}..${branch}" 2>/dev/null || echo "?");
behind=$(git rev-list --count "${branch}..${refbranch}" 2>/dev/null || echo "?");
echo "$ahead|$behind|$rest";
done \
| column -t -s"|" -R1,2,4 -T6 -N"+,-, branch,lastcommit, ,message,author";
}; r'
git config --global alias.recent-branches "$ALIAS"
echo "✓ git recent-branches installed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment