How to keep your Git repo clean after submitting a Pull Request?

If you use Git you very likely use pull requests as well. But if you don’t then read here why you should’ve.

So let’s say you do this.

git branch my-feature
git push -u origin my-feature

Then you submit a pull-request, which as a result may accept the changes and delete the branch.

Problem

What happens locally when you delete the remote branch?
Well….Nothing! The branch lives until you manually delete it.

If you’re not good at cleaning the leftovers from your current work, you’ll end up having dozens of orphaned branches. Cleaning them later is much harder…

Figure: Orphaned branches

Solution

…unless you have a tool for that. What I would expect from git to do for me with a single command ended up being a reasonably simple PowerShell script which firstly prunes the remote references and then removes the “gone” branches.

All you have to do is either:

Run GitCleanup.ps1 from a Git folderPS C:\Git\MyRepos>.\GitCleanup.ps1

or

Run GitCleanup.ps1 and specify the Git folder.PS C:\>.\GitCleanup.ps1 -Location C:\Git\MyRepo