Skip to content

Instantly share code, notes, and snippets.

@alexchristianqr
Last active April 8, 2025 17:21
Show Gist options
  • Save alexchristianqr/1ffa4187e119bb4a90cd25719ce87351 to your computer and use it in GitHub Desktop.
Save alexchristianqr/1ffa4187e119bb4a90cd25719ce87351 to your computer and use it in GitHub Desktop.
Limpiar ramas masivamente y excluir en una lista las necesarias

🧼 Git Branch Cleaner

Una herramienta de línea de comandos para limpiar ramas locales en tu repositorio Git de forma segura y personalizable.
Te permite eliminar ramas que ya no necesitas, excluyendo aquellas que están protegidas por nombre o por patrón (feat/*, fix/*, etc.).


🚀 Características

  • 🔒 Protege ramas por nombre exacto (main, master, dev, qa, etc.)
  • ✨ Soporte para patrones con comodines (feat/*, fix/*)
  • 📂 Limpieza de ramas locales
  • 🌐 Limpieza de ramas remotas (origin)
  • 🔍 Modo seguro por defecto (dry run)
  • 🗑 Eliminación automática opcional con --run
  • ✅ Salida amigable con resumen
  • 🧩 Fácil de personalizar y extender

📦 Instalación

  1. Clona o descarga el archivo git-branch-cleaner.sh en tu proyecto:
curl -O https://gist.githubusercontent.com/alexchristianqr/1ffa4187e119bb4a90cd25719ce87351/raw/git-branch-cleaner.sh
wget https://gist.githubusercontent.com/alexchristianqr/1ffa4187e119bb4a90cd25719ce87351/raw/git-branch-cleaner.sh

image

#!/bin/bash
# ===============================
# 🧼 Git Branch Cleaner v1.0
# ===============================
# Elimina ramas locales excepto las protegidas o que coinciden con patrones.
# Usa --run para ejecutar, o por defecto solo muestra (dry run).
# ===============================
# 🚫 Ramas protegidas por nombre o patrón (usa globbing)
PROTECTED_PATTERNS=("master" "main" "dev" "qa" "feat/*" "fix/*")
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
# 🔒 Verifica si una rama coincide con patrón protegido
is_protected() {
local branch=$1
for pattern in "${PROTECTED_PATTERNS[@]}"; do
if [[ "$branch" == $pattern ]]; then
return 0
fi
done
return 1
}
# 🧹 Limpia ramas locales
clean_local_branches() {
local run=$1
local deleted_count=0
local skipped_count=0
local all_branches
all_branches=$(git branch | sed 's/..//')
echo -e "${CYAN}📂 Limpieza de ramas locales:${NC}"
for branch in $all_branches; do
if is_protected "$branch"; then
echo -e "${GREEN}✔ Protegida: $branch${NC}"
((skipped_count++))
else
if [[ "$run" == "true" ]]; then
echo -e "${RED}🗑 Eliminando local: $branch${NC}"
git branch -D "$branch"
else
echo -e "${YELLOW}🔸 Se eliminaría local: $branch${NC}"
fi
((deleted_count++))
fi
done
echo -e "${CYAN}🔚 Local: $deleted_count eliminables, $skipped_count protegidas.${NC}"
}
# 🌐 Limpia ramas remotas
clean_remote_branches() {
local run=$1
local deleted_count=0
local skipped_count=0
local remote_branches
remote_branches=$(git branch -r | grep origin/ | sed 's/origin\///')
echo -e "${CYAN}🌐 Limpieza de ramas remotas (origin):${NC}"
for branch in $remote_branches; do
if is_protected "$branch"; then
echo -e "${GREEN}✔ Protegida: $branch${NC}"
((skipped_count++))
else
if [[ "$run" == "true" ]]; then
echo -e "${RED}🗑 Eliminando remoto: origin/$branch${NC}"
git push origin --delete "$branch"
else
echo -e "${YELLOW}🔸 Se eliminaría remoto: origin/$branch${NC}"
fi
((deleted_count++))
fi
done
echo -e "${CYAN}🔚 Remoto: $deleted_count eliminables, $skipped_count protegidas.${NC}"
}
# 📘 Ayuda
show_help() {
echo -e "${CYAN}Uso:${NC}"
echo " ./git-clean-branches.sh [opciones]"
echo ""
echo "Opciones:"
echo " --run Ejecuta la eliminación local"
echo " --remote También limpia ramas en origin"
echo " --help Muestra esta ayuda"
echo ""
echo "${CYAN}Protección activa:${NC}"
for pattern in "${PROTECTED_PATTERNS[@]}"; do
echo " - $pattern"
done
}
# 🚀 Lógica principal
run=false
remote=false
# Analiza argumentos
for arg in "$@"; do
case $arg in
--run) run=true ;;
--remote) remote=true ;;
--help) show_help; exit 0 ;;
*) echo -e "${RED}❌ Opción no válida: $arg${NC}"; show_help; exit 1 ;;
esac
done
# Ejecuta procesos
clean_local_branches "$run"
if [[ "$remote" == "true" ]]; then
clean_remote_branches "$run"
fi
# ========
# README
# ========
# chmod +x git-clean-branches.sh
# ./git-clean-branches.sh
# ./git-clean-branches.sh --run
# ./git-clean-branches.sh --run --remote
# ./git-clean-branches.sh --help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment