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.
What is this script?
Section titled “What is this script?”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.
Why use this script?
Section titled “Why use this script?”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.
When to use this script?
Section titled “When to use this script?”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.
Where to use this script?
Section titled “Where to use this script?”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.
Who should use this script?
Section titled “Who should use this script?”Any git user who frequently switches between branches and wants a more efficient way to do so can benefit from this script.
How to use this script?
Section titled “How to use this script?”- Copy the script: Copy the entire script provided below.
- Paste into terminal: Open your terminal and paste the script.
- 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:
# Git Branch Selector Script with Color Formatting and Help Information# Create a temporary script filetemp_script=$(mktemp)
# Write the script content to the temporary filecat << 'EOF' > "$temp_script"#!/bin/bash
# Function to display help information with colorshow_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 installedif ! command -v git &> /dev/null; then echo "$(tput setaf 1)Error: git is not installed.$(tput sgr0)" exit 1fi
# Check if inside a git repositoryif ! 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 1fi
# Display help informationshow_help
# Function to list all local branches with numbering and retain original formattinglist_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 lineextract_branch_name() { echo "$1" | awk '{print $1}'}
# Function to check for uncommitted changescheck_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 branchswitch_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 executionlist_branchescheck_uncommitted_changesswitch_branch
# Display all branches with verbose information before exitingechoecho "Final branch list:"git branch -a -vv --color=alwaysEOF
# Make the script executablechmod +x "$temp_script"
# Run the script"$temp_script"
# Clean up the temporary script filerm "$temp_script"Summary
Section titled “Summary”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.