Skip to content

Instantly share code, notes, and snippets.

@cspotcode
Created September 5, 2017 14:28
Show Gist options
  • Save cspotcode/3ebfb4e34691adbc1c7c9db84a078b87 to your computer and use it in GitHub Desktop.
Save cspotcode/3ebfb4e34691adbc1c7c9db84a078b87 to your computer and use it in GitHub Desktop.
Delete old, merged git branches from a remote
# Recipe for deleting old, merged branches from a remote repository
# delete stuff already merged into this branch...
$developBranch = 'develop'
# ...and whose most recent commit is older than this...
$ifOlderThan = [datetime]'06-01-2017'
# ...on this remote
$remote = 'origin'
# Get list of all remote branches already merged into develop
$branches = git branch --merged $developBranch -r | % { $_ -replace ' ','' } | ? { -not ( $_ -like "*HEAD*" ) -and $_ -like "$remote*" }
# Generate a list of {name, date} objects
$times = $branches | % {
[pscustomobject]@{
name = $_;
date = [DateTime](git log -1 '--pretty=format:%cI' $_)
}
}
# Drop the old, merged branches from the server
$times | ? {
$_.date.compareTo($ifOlderThan) -eq -1
} | % {
git push $remote ":$( $_.name -replace "$remote/",'' )"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment