Last active
April 22, 2024 15:26
-
-
Save jonathonherbert/3815a0c6015679ab76bd0b06a68e4acb to your computer and use it in GitHub Desktop.
Select git branch by date (requires fzf)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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