Home / Market / clean branches

clean branches

No skill description provided

claude-command★ 346920 dlgithubMITInstall in CosmosUpstream Source

Documentation & Preview

Clean Branches Command

Clean up merged and stale git branches

Instructions

Follow this systematic approach to clean up git branches: $ARGUMENTS

  1. Repository State Analysis
   # Check current status
   git status
   git branch -a
   git remote -v
   
   # Check main branch name
   git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
  1. Safety Precautions
   # Ensure clean state
   git stash push -m "Backup before branch cleanup"
   git checkout main  # or master
   git pull origin main
  1. Identify Merged Branches
   # List merged local branches
   git branch --merged main | grep -v "main\\|master\\|develop\\|\\*"
   
   # List merged remote branches
   git branch -r --merged main | grep -v "main\\|master\\|develop\\|HEAD"
  1. Identify Stale Branches
   # List branches by last commit date
   git for-each-ref --format='%(committerdate) %(authorname) %(refname)' --sort=committerdate refs/heads
   
   # Find branches older than 30 days
   git for-each-ref --format='%(refname:short) %(committerdate)' refs/heads | awk '$2 < "'$(date -d '30 days ago' '+%Y-%m-%d')'"'
  1. Interactive Branch Review
   # Check for unmerged changes
   git log main..branch-name --oneline
   
   # Show branch information
   git show-branch branch-name main
  1. Protected Branch Configuration
   # Example protected branches
   PROTECTED_BRANCHES=("main" "master" "develop" "staging" "production")
  1. Local Branch Cleanup
   # Delete merged branches (interactive)
   git branch --merged main | grep -v "main\\|master\\|develop\\|\\*" | xargs -n 1 -p git branch -d
   
   # Force delete if needed (use with caution)
   git branch -D branch-name
  1. Remote Branch Cleanup
   # Prune remote tracking branches
   git remote prune origin
   
   # Delete remote branch
   git push origin --delete branch-name
   
   # Remove local tracking of deleted remote branches
   git branch -dr origin/branch-name
  1. Automated Cleanup Script
   #!/bin/bash
   
   # Git branch cleanup script
   set -e
   
   # Configuration
   MAIN_BRANCH="main"
   PROTECTED_BRANCHES=("main" "master" "develop" "staging" "production")
   STALE_DAYS=30
   
   # Functions
   is_protected() {
       local branch=$1
       for protected in "${PROTECTED_BRANCHES[@]}"; do
           if [[ "$branch" == "$protected" ]]; then
               return 0
           fi
       done
       return 1
   }
   
   # Switch to main branch
   git checkout $MAIN_BRANCH
   git pull origin $MAIN_BRANCH
   
   # Clean up merged branches
   echo "Cleaning up merged branches..."
   merged_branches=$(git branch --merged $MAIN_BRANCH | grep -v "\\*\\|$MAIN_BRANCH")
   
   for branch in $merged_branches; do
       if ! is_protected "$branch"; then
           echo "Deleting merged branch: $branch"
           git branch -d "$branch"
       fi
   done
   
   # Prune remote tracking branches
   echo "Pruning remote tracking branches..."
   git remote prune origin
   
   echo "Branch cleanup completed!"
  1. Team Coordination
  1. Branch Naming Convention Cleanup
  1. Verification and Validation
    # Verify cleanup results
    git branch -a
    git remote show origin
  1. Documentation and Reporting
  1. Rollback Procedures
    # Recover deleted branch using reflog
    git reflog --no-merges --since="2 weeks ago"
    git checkout -b recovered-branch commit-hash
  1. Automation Setup
  1. Best Practices Implementation

Advanced Cleanup Options:

# Clean up all merged branches except protected ones
git branch --merged main | grep -E "^  (feature|hotfix|bugfix)/" | xargs -n 1 git branch -d

# Interactive cleanup with confirmation
git branch --merged main | grep -v "main\|master\|develop" | xargs -n 1 -p git branch -d

# Batch delete remote branches
git branch -r --merged main | grep origin | grep -v "main\|master\|develop\|HEAD" | cut -d/ -f2- | xargs -n 1 git push origin --delete

# Clean up branches older than specific date
git for-each-ref --format='%(refname:short) %(committerdate:short)' refs/heads | awk '$2 < "2023-01-01"' | cut -d' ' -f1 | xargs -n 1 git branch -D

Remember to: