以下の記事にあるコマンドをWindows上で実行したくなったので、PowerShellに書き換えました。
Gitでマージ済みブランチを一括削除
前提
PowerShell Core 7 がインストール済であること
コマンド
記事と同様に現在のブランチとdevelop,masterは除外しています。
git branch --merged `
| Select-String -NotMatch -Pattern "(\*|develop|master)" `
| %{ git branch -d $_.ToString().Trim() }
モジュール化
PowerShell Core を起ち上げ、$env:PSModulePath
と入力します。
出力されたパスがモジュールの格納場所になります。今回は$HOME\Documents\PowerShell\Modules
に以下のpsm1ファイルを保存します。
GitExtension.psm1
function Remove-GitBranchMerged {
# 引数で指定されていなければカレントディレクトリを対象にする
$dir = $args[0]
if ($args.count -eq 0) {
$dir = (Get-Location)
}
Write-Host ("target directory:" + $dir)
Push-Location $dir
git fetch --prune origin
git branch --merged `
| Select-String -NotMatch -Pattern "(\*|develop|master)" `
| ForEach-Object{ git branch -d $_.ToString().Trim() }
Pop-Location
}
PowerShell Core を起ち上げなおすとRemove-GitBranchMerged
が使えるようになるはずです。