๐Ÿงน Skrip Otomatis Bersihkan Branch Git yang Sudah Di-Merge

Dengan dukungan opsi --dry-run dan --auto, aman & efisien!

Kalau kamu sering kerja bareng tim dan repo Git kamu makin penuh sama branch lama, skrip ini bakal jadi penyelamat. ๐Ÿ˜Ž Dengan skrip Bash ini, kamu bisa menampilkan daftar branch yang sudah di-merge, konfirmasi sebelum hapus, bahkan bersihkan juga di remote kalau mau.

๐Ÿ’พ Isi Skrip: clean-merged-branches.sh

#!/usr/bin/env bash
# ===============================================
# Bersihkan branch Git yang sudah di-merge
# Versi: 2.0
# Penulis: Frijal (https://dalam.web.id)
# ===============================================
set -e
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
RED="\033[1;31m"
CYAN="\033[1;36m"
NC="\033[0m"
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
 echo -e "${RED}โŒ Bukan repository Git.${NC}"
 exit 1
fi
MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
MAIN_BRANCH=${MAIN_BRANCH:-main}
AUTO=false
DRY_RUN=false
# Baca argumen
for arg in "$@"; do
 case $arg in
 --auto) AUTO=true ;;
 --dry-run) DRY_RUN=true ;;
 esac
done
echo -e "${CYAN}๐Ÿ“ฆ Branch utama terdeteksi: ${MAIN_BRANCH}${NC}"
git checkout "$MAIN_BRANCH" >/dev/null 2>&1 || { echo -e "${RED}โŒ Gagal checkout.${NC}"; exit 1; }
echo -e "${YELLOW}๐Ÿ”„ Sinkronisasi dengan remote...${NC}"
git fetch -p
MERGED_BRANCHES=$(git branch --merged "$MAIN_BRANCH" | grep -vE "^\*|${MAIN_BRANCH}|master|develop" || true)
if [ -z "$MERGED_BRANCHES" ]; then
 echo -e "${GREEN}โœ… Tidak ada branch yang sudah di-merge.${NC}"
 exit 0
fi
echo -e "\n${CYAN}๐Ÿ“‹ Branch yang sudah di-merge:${NC}"
echo "$MERGED_BRANCHES"
if $DRY_RUN; then
 echo -e "\n${YELLOW}๐Ÿงช Mode dry-run aktif: tidak ada branch yang dihapus.${NC}"
 exit 0
fi
if ! $AUTO; then
 read -rp "$(echo -e "${YELLOW}โš ๏ธ Hapus branch di atas? (y/N): ${NC}")" confirm
 [[ ! "$confirm" =~ ^[Yy]$ ]] && echo -e "${RED}๐Ÿšซ Dibatalkan.${NC}" && exit 0
fi
echo -e "${YELLOW}๐Ÿงน Menghapus branch lokal...${NC}"
while read -r branch; do
 [ -z "$branch" ] && continue
 git branch -d "$branch" || echo -e "${RED}โŒ Gagal hapus $branch${NC}"
done <<<"$MERGED_BRANCHES"
if $AUTO; then
 DELETE_REMOTE=true
else
 read -rp "$(echo -e "${YELLOW}๐ŸŒ Hapus branch remote juga? (y/N): ${NC}")" confirm_remote
 [[ "$confirm_remote" =~ ^[Yy]$ ]] && DELETE_REMOTE=true
fi
if [ "$DELETE_REMOTE" = true ]; then
 echo -e "${YELLOW}๐ŸŒ Menghapus branch remote...${NC}"
 while read -r branch; do
 REMOTE_EXISTS=$(git ls-remote --heads origin "$branch")
 if [ -n "$REMOTE_EXISTS" ]; then
 echo -e "${CYAN}๐Ÿ”ธ Menghapus origin/$branch${NC}"
 git push origin --delete "$branch" || echo -e "${RED}โŒ Gagal hapus remote: $branch${NC}"
 fi
 done <<<"$MERGED_BRANCHES"
fi
echo -e "\n${GREEN}โœ… Selesai! Semua branch yang sudah di-merge dibersihkan.${NC}"

โš™๏ธ Cara Pakai

  1. Simpan skrip di file clean-merged-branches.sh
  2. Jadikan executable:
    chmod +x clean-merged-branches.sh
  3. Jalankan di folder Git project:
    ./clean-merged-branches.sh

Kamu juga bisa jalankan dengan opsi tambahan:

Contoh:

./clean-merged-branches.sh --dry-run
./clean-merged-branches.sh --auto

๐Ÿ’ก Tips Aman Sebelum Bersih-Bersih