Skip to content

Instantly share code, notes, and snippets.

@jonathonherbert
Last active April 22, 2024 15:26
Show Gist options
  • Save jonathonherbert/3815a0c6015679ab76bd0b06a68e4acb to your computer and use it in GitHub Desktop.
Save jonathonherbert/3815a0c6015679ab76bd0b06a68e4acb to your computer and use it in GitHub Desktop.
Select git branch by date (requires fzf)
fbr() {
local branches branch
branches=$(git reflog show --pretty=format:'%gs ~ %gd' --date=relative \
| grep 'checkout:' \
| grep -oE '[^ ]+ ~ .*' \
| awk -F~ '!seen[$1]++' \
| head -n 20 \
| awk -F' ~ HEAD@{' '{printf(" \033[33m%s: \033[37m %s\033[0m\n", substr($2, 1, length($2)-1), $1)}' \
| nl \
| sort -nr \
| cut -f 2-) &&
branch=$(echo "$branches" |
fzf --ansi -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment