accounts/scripts/git-branch-keeper.sh
2025-10-08 13:48:17 +08:00

41 lines
970 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# =============================================
# 🧹 git-branch-keeper.sh
# 保留 main 与所有 release/* 分支
# 清理多余本地和远程分支
# =============================================
set -e
echo ">>> Fetching and pruning remote branches..."
git fetch --all --prune
echo ">>> Cleaning local branches..."
for branch in $(git branch | sed 's/*//'); do
case "$branch" in
main|HEAD|release/*)
echo "✅ 保留本地分支:$branch"
;;
*)
echo "🗑️ 删除本地分支:$branch"
git branch -D "$branch"
;;
esac
done
echo ">>> Cleaning remote branches..."
for branch in $(git branch -r | sed 's/origin\///'); do
case "$branch" in
HEAD|main|release/*)
echo "✅ 保留远程分支origin/$branch"
;;
*)
echo "🗑️ 删除远程分支origin/$branch"
git push origin --delete "$branch" || true
;;
esac
done
echo "✅ 分支清理完成!"