Git Tip: Deleting Old Local Branches With PowerShell

So I was reading Git Tip: Deleting Old Local Branches, but the code was for Bash/Linux. My first instinct in all of these cases is to translate it into PowerShell.  I am shamelessly stealing the format of the original post as I liked the way he laid it out.

Update:  I have a script that can do all of this here GitDeleteOldLocalBranches.ps1.

0) Prune Remote Branches

>git remote prune origin

Read more about git remote prune here

Deletes all stale remote-tracking branches under . These stale branches have already been removed from the remote repository referenced by , but are still locally available in “remotes/”.

1) List local git branches

git branch -vv will list all local branches along with some additional information such as their related upstream/remote branch and latest commit message

> git branch -vv
#…
feature/some-local-only-feature cba8191 Some commit message
feature/some-old-feature cba2191 [origin/feature/some-old-feature: gone] Some commit message about some old feature
feature/some-active-feature wba2191 [origin/feature/some-active-feature: ahead 40, behind 10] Some active feature branch
#…

2) Filter git branches down to only those with deleted upstream/remote counterparts

Next, we pipe the output from git branch -vv into grep ‘origin/.*: gone]’. This filters our list down to only lines that match the regex origin/.*: gone] leaving us with

> git branch -vv | where {$_ -match '\[origin/.*: gone\]'}
#…
feature/some-old-feature cba2191 [origin/feature/some-old-feature: gone] Some commit message about some old feature
#…
Read more about using regular expressions with PowerShell here.

3) Pluck out branch names from output

Piping that into split()[0] cleans up our output so we end up with a branch name per line.

> git branch -vv | where {$_ -match '\[origin/.*: gone\]'} | foreach {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[0]}'
#…
feature/some-old-feature cba2191 [origin/feature/some-old-feature: gone] Some commit message about some old feature
#…
This is because split translates the string to an array (items being separated by space(s)) and [0] is the first item, which is the branch name. We also use the RemoveEmptyEntries option so that any leading/trailing spaces are removed.  Example:

> git branch -vv | where {$_ -match '\[origin/.*: gone\]'} | foreach {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')}
[0]: feature/some-old-feature
[1]: cba2191
[2]: [origin/feature/some-old-feature:
[3]: gone]
[4]: First
[5]: words
[6]: in_commit_message…

4) Delete the branches!

Next, we pipe our filtered down, cleaned up git branches list into git branch -d (short for –delete) and say our final goodbyes.

>git branch -vv | where {$_ -match '\[origin/.*: gone\]'} | foreach {git branch -d ($_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[0])}

Conclusion:

You should always run step 0 and step 3 to validate the branches you are going to be deleting before running step 4.  Don’t say you weren’t warned.

This does not clean up any local branches that were never pushed to the remote server.  That’s a script for another day.

2 comments

Leave a Reply to Carlos Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s