Skip to content

Instantly share code, notes, and snippets.

@miyl
Last active March 28, 2024 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyl/0e5f536c8e3d0ade2a14a4c5860d87c6 to your computer and use it in GitHub Desktop.
Save miyl/0e5f536c8e3d0ade2a14a4c5860d87c6 to your computer and use it in GitHub Desktop.
Improve checking out branches in git via fzf
# Description:
# 1. Check if inside git repository.
# 2. If so list all branches except the current including its remote equivalent.
# 3. Remove duplicates that are both local and remote.
# 4. Send it to fzf, and checkout the branch chosen from there.
git config --global alias.co '! git rev-parse --is-inside-work-tree > /dev/null 2>&1 && git branch -a | sed -e "\,HEAD\|$(git branch --show-current),d" | sed "s,remotes/origin/,," | sort | uniq | fzf | xargs -r git checkout'
# Here's a cowsay/lolcat version for the adventurous:
# git config --global alias.co '! a=$(git rev-parse --is-inside-work-tree > /dev/null 2>&1 && git branch -a | sed -e "\,HEAD\|$(git branch --show-current),d" | sed "s,remotes/origin/,," | sort | uniq | fzf | xargs -r git checkout); [ -n "$a" ] && echo "$a" | cowsay -n | lolcat'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment