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://frijal.pages.dev)
# ===============================================
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
- Simpan skrip di file
clean-merged-branches.sh
- Jadikan executable:
chmod +x clean-merged-branches.sh
- Jalankan di folder Git project:
./clean-merged-branches.sh
Kamu juga bisa jalankan dengan opsi tambahan:
--dry-run
โ hanya tampilkan branch yang akan dihapus tanpa eksekusi--auto
โ hapus langsung tanpa konfirmasi (cocok untuk CI/CD)
Contoh:
./clean-merged-branches.sh --dry-run
./clean-merged-branches.sh --auto
๐ก Tips Aman Sebelum Bersih-Bersih
- Pastikan semua perubahan sudah di-merge ke
main
atau branch utama kamu. - Gunakan
--dry-run
dulu untuk memastikan daftar yang aman. - Backup branch penting dengan
git push origin nama_branch
sebelum hapus.