Skip to content

Git: Branch Selector Script

Source: Notion | Last edited: 2024-09-07 | ID: 590d57d8-559...


This tutorial will guide you through the usage of a Git Branch Selector Script that allows you to list and switch between local git branches with ease. The script includes color formatting and help information to enhance user experience.

This script is a Bash script that lists all local git branches with numbering and allows you to select one to switch to. It also checks for uncommitted changes and provides help information.

Switching between branches in a git repository can be cumbersome, especially when there are many branches. This script simplifies the process by providing a numbered list of branches and allowing you to select one by its number.

Use this script whenever you need to switch between branches in a git repository. It is particularly useful when you have multiple branches and want a quick way to navigate between them.

This script should be run inside a git repository. It checks if git is installed and if the script is being run inside a git repository before proceeding.

Any git user who frequently switches between branches and wants a more efficient way to do so can benefit from this script.

  1. Copy the script: Copy the entire script provided below.
  2. Paste into terminal: Open your terminal and paste the script.
  3. Run the script: The script will create a temporary file, write the script content to it, make it executable, and then run it. Here is the script:
Terminal window
# Git Branch Selector Script with Color Formatting and Help Information
# Create a temporary script file
temp_script=$(mktemp)
# Write the script content to the temporary file
cat << 'EOF' > "$temp_script"
#!/bin/bash
# Function to display help information with color
show_help() {
echo "$(tput setaf 3)Usage:$(tput sgr0) This script lists all local git branches with numbering and allows you to select one to switch to."
echo "$(tput setaf 3)To quit, enter 'q' when prompted to select a branch.$(tput sgr0)"
echo
}
# Check if git is installed
if ! command -v git &> /dev/null; then
echo "$(tput setaf 1)Error: git is not installed.$(tput sgr0)"
exit 1
fi
# Check if inside a git repository
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
echo "$(tput setaf 1)Error: This script must be run inside a git repository.$(tput sgr0)"
exit 1
fi
# Display help information
show_help
# Function to list all local branches with numbering and retain original formatting
list_branches() {
branches=()
index=0
echo "Available branches:"
while IFS= read -r line; do
echo "$(tput setaf 2)$index) $(tput sgr0)$line"
branches+=("$line")
((index++))
done < <(git branch -vv --color=always)
echo
# Check if there are no branches available
if [ ${#branches[@]} -eq 0 ]; then
echo "$(tput setaf 1)Warning: No branches available.$(tput sgr0)"
exit 1
fi
}
# Function to extract branch name from the selected line
extract_branch_name() {
echo "$1" | awk '{print $1}'
}
# Function to check for uncommitted changes
check_uncommitted_changes() {
if [[ -n $(git status --porcelain) ]]; then
echo "$(tput setaf 1)Warning: You have uncommitted changes.$(tput sgr0)"
git status --short
read -p "Do you want to proceed with switching to the branch? (y/n): " proceed
if [[ "$proceed" != "y" ]]; then
echo "Switch cancelled."
exit 1
fi
fi
}
# Function to handle user input and switch to the selected branch
switch_branch() {
read -p "Select the number of the branch you want to switch to (or 'q' to quit): " branch_number
# Check if the user wants to quit
if [[ "$branch_number" == "q" ]]; then
echo "Exiting."
exit 0
fi
# Check if the input is a valid number
if ! [[ "$branch_number" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a number."
return 1
fi
# Check if the selected number is within the range
if [ "$branch_number" -ge "${#branches[@]}" ]; then
echo "Invalid selection. Please select a valid branch number."
return 1
fi
# Extract the branch name from the selected line
selected_branch=$(extract_branch_name "${branches[$branch_number]}")
# Confirm branch switch
read -p "Are you sure you want to switch to branch '$selected_branch'? (y/n): " confirm
if [[ "$confirm" != "y" ]]; then
echo "Switch cancelled."
return 1
fi
# Switch to the selected branch
git switch "$selected_branch"
}
# Main script execution
list_branches
check_uncommitted_changes
switch_branch
# Display all branches with verbose information before exiting
echo
echo "Final branch list:"
git branch -a -vv --color=always
EOF
# Make the script executable
chmod +x "$temp_script"
# Run the script
"$temp_script"
# Clean up the temporary script file
rm "$temp_script"

This script provides a convenient way to list and switch between git branches with color-coded output and helpful prompts. By following the steps above, you can easily integrate this script into your workflow and improve your git branch management.