Last active
March 1, 2019 12:02
-
-
Save Szer/7f347ea11e7f5ee226b80e66acfa2967 to your computer and use it in GitHub Desktop.
Deleting old remote branches which have been merged into master
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CleanupRepo { | |
param([string]$repo) | |
<#work directory should be repo root#> | |
cd $repo | |
git fetch "origin" | |
git reset --hard origin/master | |
"getting branch list..." | |
<#listing all remote branches which were merged into master#> | |
$branchesToDelete = | |
git branch -r --merged ` | |
| select -First 0 <#delete this line when go to production#> ` | |
<#we don't want to delete origin/master#> ` | |
| ?{ $_ -notmatch 'origin/master'} ` | |
| %{ $_.Trim() } ` | |
` | |
<#getting branch name and last commit date (if any) since 1 month#> ` | |
| Select-Object @{ n="Branch"; e={$_} }, ` | |
@{ n="LastCommitDate"; e={git log -1 --since='1 month ago' --format="%aI" -s $_} } ` | |
` | |
<#taking branches which DON'T have recent commits#> ` | |
| ?{ [string]::IsNullOrEmpty($_.LastCommitDate) } ` | |
| %{ $_.Branch.replace("origin/","") } | |
workflow DeleteBranches { | |
param([string[]] $branches, | |
[string] $repo) | |
foreach -parallel -throttlelimit 10 ($branch in $branches) { | |
InlineScript { | |
Set-Location $Using:repo | |
"working on $Using:branch" | |
<#delete reference from origin#> | |
git push --delete origin $Using:branch | |
} | |
} | |
} | |
DeleteBranches -branches $branchesToDelete -repo $repo | |
} | |
CleanupRepo "C:\dev\superman_master" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment