Skip to content

Instantly share code, notes, and snippets.

@mislav
Created November 19, 2015 15:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mislav/4452431b8985d8b2ee8b to your computer and use it in GitHub Desktop.
Save mislav/4452431b8985d8b2ee8b to your computer and use it in GitHub Desktop.
Show list of recently checked-out branches in reverse-chronological order
#!/bin/bash
set -e
git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD | {
seen=":"
git_dir="$(git rev-parse --git-dir)"
while read line; do
date="${line%%|*}"
branch="${line##* }"
if ! [[ $seen == *:"${branch}":* ]]; then
seen="${seen}${branch}:"
if [ -f "${git_dir}/refs/heads/${branch}" ]; then
printf "%s\t%s\n" "$date" "$branch"
fi
fi
done
}
@PeterPerhac
Copy link

LOVE IT! Thanks

@brandondrew
Copy link

This has a bug somewhere, as it regularly misses branches of mine. I've increased it to -n10000 to ensure that failing to look far enough back into the reflog is not the source of the problem. If I find the bug I'll post back here.

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