Skip to content

Instantly share code, notes, and snippets.

@dennisroche
Created September 23, 2016 06:04
Show Gist options
  • Save dennisroche/1df531c62acc7ba01577cd2a7b7a3224 to your computer and use it in GitHub Desktop.
Save dennisroche/1df531c62acc7ba01577cd2a7b7a3224 to your computer and use it in GitHub Desktop.
A posh-git script that identifies local branches that have been merged to `master`.
function Remove-GitMergedBranches {
Write-Output "Fetching latest"
git fetch origin --prune
$currentBranch = & git rev-parse --abbrev-ref HEAD
if ($currentBranch -ne "master") {
Write-Output "Changing branch to master"
git checkout master
}
# Merge with origin with **fast-forward only** so that `git` will stop if there is an error
Write-Output "Fast-Forward merge with origin/master"
git merge origin/master --ff-only
if ($LASTEXITCODE -ne 0) {
throw "git exit code: $LASTEXITCODE"
}
git branch | ? { $_ -notmatch '(^\*)' } | ForEach-Object {
$branch = $_.trim()
Write-Output "$branch"
try {
git merge --no-ff --no-commit $branch | Out-Null
git diff --staged --exit-code --quiet
if ($LASTEXITCODE -eq 0) {
Write-Output "Would delete '$branch'"
# git branch -D $branch
}
} finally {
git merge --abort | Out-Null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment