Skip to content

Instantly share code, notes, and snippets.

@schtibe
Last active February 1, 2024 12:37
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 schtibe/f24cb24c3b47b2c5e532799cc2e42c8b to your computer and use it in GitHub Desktop.
Save schtibe/f24cb24c3b47b2c5e532799cc2e42c8b to your computer and use it in GitHub Desktop.
Git switch branch helper
#!/bin/bash
git fetch > /dev/null
# Get the list of branches from "git branch -a" using fzf for interactive selection
selected_branch=$(git branch -a | grep -v HEAD | sed 's/^[[:space:]]*//' | fzf --ansi --prompt="Select a branch (Use arrow keys): ")
if [ -n "$selected_branch" ]; then
if [ "$selected_branch" == "Exit" ]; then
echo "Exiting..."
else
echo "You selected branch: $selected_branch"
cleaned_branch_name=${selected_branch/remotes\//}
cleaned_branch_name=${cleaned_branch_name/origin\//}
echo "Checking out $cleaned_branch_name"
git switch $cleaned_branch_name
fi
else
echo "No branch selected. Exiting..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment