Skip to content

Instantly share code, notes, and snippets.

@ksuzushima
Created June 22, 2025 14:22
Show Gist options
  • Save ksuzushima/447d590194db9b8de7c95b1ceb04593a to your computer and use it in GitHub Desktop.
Save ksuzushima/447d590194db9b8de7c95b1ceb04593a to your computer and use it in GitHub Desktop.
Git cleanup merged branches - Commands to safely delete merged Git branches

Git Cleanup Merged Branches / マージ済みブランチの削除

English

Delete all merged branches except the current branch

git branch --merged | grep -v "^\*" | xargs -r git branch -d

Delete merged branches with exclusions

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

Customizable version

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

Command explanation

  • git branch --merged: Lists branches that have been merged into the current branch
  • grep -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)

Tips

Dry run

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"

Force delete

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

Alias setup

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

特定のブランチを除外して削除

特定のブランチ(例:mainmasterdevelopstaging)を保護したい場合:

# 拡張正規表現を使用(-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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment