Skip to content

Instantly share code, notes, and snippets.

@plucury
Created April 11, 2025 05:04
Show Gist options
  • Save plucury/f192d7bb00a6afd54fb5b96aa6a2f57a to your computer and use it in GitHub Desktop.
Save plucury/f192d7bb00a6afd54fb5b96aa6a2f57a to your computer and use it in GitHub Desktop.
Recent used git branch
gbf() {
if git show-ref --verify --quiet refs/heads/main; then
main_branch="main"
elif git show-ref --verify --quiet refs/heads/master; then
main_branch="master"
else
return 1
fi
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ \
| awk '!seen[$0]++' | head -n 20 | while read branch; do
if git show-ref --verify --quiet "refs/heads/$branch"; then
base=$(git merge-base "$branch" "$main_branch" 2>/dev/null)
if [ -z "$base" ]; then continue; fi
first_commit=$(git log "$branch" --no-merges --reverse --pretty=format:'%H' --not "$base" 2>/dev/null | head -n 1)
if [ -n "$first_commit" ]; then
msg=$(git log -1 --format=%s "$first_commit")
date=$(git log -1 --format=%ad --date=short "$first_commit")
printf "%-30s | %-10s | %s\n" "$branch" "$date" "$msg"
fi
fi
done | fzf --prompt="Checkout branch: " | awk '{print $1}' | xargs git checkout
}
# mac
brew install fzf
# Arch
sudo pacman -S fzf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment