LoginSignup
5
2

More than 3 years have passed since last update.

PowerShellでマージ済のgitブランチを一括削除

Last updated at Posted at 2017-11-09

以下の記事にあるコマンドを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が使えるようになるはずです。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2