Created
September 12, 2025 07:35
-
-
Save natansdj/4568563fcbbbafcfdbf3d41456d82762 to your computer and use it in GitHub Desktop.
zsh plugin git checkout all plugin, Checkout the same branch across all repositories in current directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Git Checkout All Plugin | |
| # Author: | |
| # Description: Checkout the same branch across all repositories in current directory | |
| # | |
| # Features of this plugin: | |
| # | |
| # Enhanced error handling - Checks if directories and branches exist | |
| # Remote branch support - Automatically creates local branches from remote if they exist | |
| # Progress indicators - Shows success/failure with emojis and counts | |
| # Multiple useful functions: | |
| # | |
| # git-checkout-all (alias: gcoa) - Main checkout function | |
| # git-list-branches-all (alias: glba) - List all branches | |
| # git-status-all (alias: gsa) - Show current branch status | |
| # git-checkout-all-help - Show help information | |
| # | |
| # Clean output - Formatted output with clear indicators | |
| # Summary statistics - Shows how many repositories were successfully updated | |
| # The plugin will automatically handle cases where branches exist only on remote, provide clear feedback for each repository, and give you a summary of the operation. | |
| # Main function to checkout branch in all repositories | |
| git-checkout-all() { | |
| if [ -z "$1" ]; then | |
| echo "Usage: git-checkout-all <branch-name>" | |
| echo "Example: git-checkout-all main" | |
| return 1 | |
| fi | |
| local branch_name="$1" | |
| local base_path="$(pwd)" | |
| local success_count=0 | |
| local total_repos=0 | |
| echo "π Searching for git repositories in $(pwd)..." | |
| echo "πΏ Attempting to checkout branch: $branch_name" | |
| echo "βββββββββββββββββββββββββββββββββββββββββββββββββ" | |
| for dir in "$base_path"/*/; do | |
| if [ -d "$dir/.git" ]; then | |
| local repo_name=$(basename "$dir") | |
| total_repos=$((total_repos + 1)) | |
| echo -n "π $repo_name: " | |
| (cd "$dir" && { | |
| # Check if branch exists locally | |
| if git rev-parse --verify "$branch_name" >/dev/null 2>&1; then | |
| if git checkout "$branch_name" >/dev/null 2>&1; then | |
| echo "β Success" | |
| success_count=$((success_count + 1)) | |
| else | |
| echo "β Failed to checkout" | |
| fi | |
| # Check if branch exists on remote | |
| elif git ls-remote --heads origin "$branch_name" 2>/dev/null | grep -q "$branch_name"; then | |
| if git checkout -b "$branch_name" "origin/$branch_name" >/dev/null 2>&1; then | |
| echo "β Success (created from remote)" | |
| success_count=$((success_count + 1)) | |
| else | |
| echo "β Failed to create from remote" | |
| fi | |
| else | |
| echo "β οΈ Branch not found" | |
| fi | |
| }) | |
| fi | |
| done | |
| echo "βββββββββββββββββββββββββββββββββββββββββββββββββ" | |
| echo "π Summary: $success_count/$total_repos repositories updated" | |
| if [ $total_repos -eq 0 ]; then | |
| echo "β οΈ No git repositories found in $(pwd)" | |
| fi | |
| } | |
| # Alias for shorter command | |
| alias gcoa='git-checkout-all' | |
| # Function to list all branches across repositories | |
| git-list-branches-all() { | |
| local base_path="$(pwd)" | |
| echo "πΏ Listing branches across all repositories in $(pwd):" | |
| echo "βββββββββββββββββββββββββββββββββββββββββββββββββ" | |
| for dir in "$base_path"/*/; do | |
| if [ -d "$dir/.git" ]; then | |
| local repo_name=$(basename "$dir") | |
| echo "π $repo_name:" | |
| (cd "$dir" && { | |
| local current_branch=$(git branch --show-current 2>/dev/null || echo "") | |
| if [ -n "$current_branch" ]; then | |
| # Use a safer approach to highlight current branch | |
| git branch -a 2>/dev/null | while IFS= read -r line; do | |
| # Remove leading/trailing whitespace and check if it's the current branch | |
| clean_line=$(echo "$line" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//') | |
| if [[ "$line" == *"* "* ]]; then | |
| # This is the current branch line | |
| echo " β ${clean_line#* }" | |
| else | |
| echo " $line" | |
| fi | |
| done | |
| else | |
| git branch -a 2>/dev/null | sed 's/^/ /' 2>/dev/null || echo " (no branches found)" | |
| fi | |
| }) | |
| echo | |
| fi | |
| done | |
| } | |
| # Alias for branch listing | |
| alias glba='git-list-branches-all' | |
| # Function to show current branch in all repositories | |
| git-status-all() { | |
| local base_path="$(pwd)" | |
| echo "π Current branch status across all repositories:" | |
| echo "βββββββββββββββββββββββββββββββββββββββββββββββββ" | |
| for dir in "$base_path"/*/; do | |
| if [ -d "$dir/.git" ]; then | |
| local repo_name=$(basename "$dir") | |
| echo -n "π $repo_name: " | |
| (cd "$dir" && { | |
| local current_branch=$(git branch --show-current 2>/dev/null || echo "detached") | |
| local status=$(git status --porcelain 2>/dev/null || echo "") | |
| if [ -n "$status" ]; then | |
| echo "πΏ $current_branch (π uncommitted changes)" | |
| else | |
| echo "πΏ $current_branch (β¨ clean)" | |
| fi | |
| }) | |
| fi | |
| done | |
| } | |
| # Alias for status check | |
| alias gsa='git-status-all' | |
| # Help function | |
| git-checkout-all-help() { | |
| echo "Git Checkout All Plugin - Available Commands:" | |
| echo "βββββββββββββββββββββββββββββββββββββββββββββββββ" | |
| echo "git-checkout-all <branch> - Checkout branch in all repos (alias: gcoa)" | |
| echo "git-list-branches-all - List all branches in all repos (alias: glba)" | |
| echo "git-status-all - Show current branch status (alias: gsa)" | |
| echo "git-checkout-all-help - Show this help message" | |
| echo "" | |
| echo "Examples:" | |
| echo " gcoa main # Checkout main branch in all repos in current directory" | |
| echo " gcoa feature/new-feature # Checkout feature branch in all repos in current directory" | |
| echo " glba # List all branches in current directory repos" | |
| echo " gsa # Show current status of repos in current directory" | |
| echo "" | |
| echo "Note: All commands work on subdirectories of the current working directory" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment