git branch --merged | grep -v "^\*" | xargs -r git branch -d
If you want to protect specific branches (e.g., main
, master
, develop
, staging
):
# Using extended regex (-E option)
git branch --merged | grep -v -E "^\*|main|master|develop|staging" | xargs -r git branch -d
# Without -E option (using basic regex)
git branch --merged | grep -v "^\*" | grep -v "main" | grep -v "master" | grep -v "develop" | grep -v "staging" | xargs -r git branch -d
Replace PROTECTED_BRANCHES
with your protected branch names:
# Define your protected branches
PROTECTED_BRANCHES="main|master|develop|staging"
# Delete merged branches
git branch --merged | grep -v -E "^\*|${PROTECTED_BRANCHES}" | xargs -r git branch -d
git branch --merged
: Lists branches that have been merged into the current branchgrep -v
: Inverts the match (excludes matching lines)grep -E
: Enables extended regular expressions for pattern matching^\*
: Matches lines starting with * (current branch)|
: OR operator in regex (requires -E option)xargs -r
: Converts input into arguments for the next command (-r prevents execution if input is empty)git branch -d
: Deletes branches (safe deletion - only merged branches)
Before actually deleting, you can check which branches will be deleted:
# Show branches that would be deleted
git branch --merged | grep -v -E "^\*|main|master|develop|staging"
If you need to delete unmerged branches (use with caution):
# Use -D instead of -d
git branch --merged | grep -v -E "^\*|main|master" | xargs -r git branch -D
Add to your .gitconfig
or .bashrc
:
# Git alias
git config --global alias.cleanup '!git branch --merged | grep -v -E "^\*|main|master|develop" | xargs -r git branch -d'
# Bash alias
alias git-cleanup='git branch --merged | grep -v -E "^\*|main|master|develop" | xargs -r git branch -d'
git branch --merged | grep -v "^\*" | xargs -r git branch -d
特定のブランチ(例:main
、master
、develop
、staging
)を保護したい場合:
# 拡張正規表現を使用(-Eオプション)
git branch --merged | grep -v -E "^\*|main|master|develop|staging" | xargs -r git branch -d
# -Eオプションなし(基本正規表現)
git branch --merged | grep -v "^\*" | grep -v "main" | grep -v "master" | grep -v "develop" | grep -v "staging" | xargs -r git branch -d
PROTECTED_BRANCHES
を保護したいブランチ名に置き換えてください:
# 保護するブランチを定義
PROTECTED_BRANCHES="main|master|develop|staging"
# マージ済みブランチを削除
git branch --merged | grep -v -E "^\*|${PROTECTED_BRANCHES}" | xargs -r git branch -d
git branch --merged
: 現在のブランチにマージ済みのブランチ一覧を表示grep -v
: マッチを反転(マッチする行を除外)grep -E
: 拡張正規表現を有効化^\*
: アスタリスクで始まる行にマッチ(現在のブランチ)|
: 正規表現のOR演算子(-Eオプションが必要)xargs -r
: 入力を次のコマンドの引数に変換(-rは入力が空の場合実行しない)git branch -d
: ブランチを削除(安全な削除 - マージ済みのみ)
削除前に、どのブランチが削除されるか確認できます:
# 削除対象のブランチを表示
git branch --merged | grep -v -E "^\*|main|master|develop|staging"
マージされていないブランチも削除する場合(注意して使用):
# -d の代わりに -D を使用
git branch --merged | grep -v -E "^\*|main|master" | xargs -r git branch -D
.gitconfig
または.bashrc
に追加:
# Git エイリアス
git config --global alias.cleanup '!git branch --merged | grep -v -E "^\*|main|master|develop" | xargs -r git branch -d'
# Bash エイリアス
alias git-cleanup='git branch --merged | grep -v -E "^\*|main|master|develop" | xargs -r git branch -d'