GNU/Linux screen-256color bash 122 views

#!/bin/bash

# Fetch remote branches silently
git fetch --all --quiet

# List local + remote branches (strip 'origin/' duplicates)
branch=$(git for-each-ref --format='%(refname:short)' refs/heads refs/remotes |
sed 's#^origin/##' | sort -u |
fzf --print-query --prompt="Checkout or create> " --reverse |
awk 'NF {line=$0} END{print line}')

# Nothing selected? Abort
[[ -z "$branch" ]] && return 0

if git show-ref --verify --quiet "refs/heads/$branch"; then
    # Local branch exists → checkout normally
    git checkout "$branch"
elif git show-ref --verify --quiet "refs/remotes/origin/$branch"; then
    # Exists on remote → create local tracking branch
    git checkout -t "origin/$branch"
else
    # Doesn't exist anywhere → create new branch
    git checkout -b "$branch"
fi

More recordings by kllmanu

bs 0:33

by kllmanu

arrow 0:57

by kllmanu