Introduction
In our team we had some trouble with old Git branches not being cleaned up although they were merged in the past. Jenkins (on Windows) would then also try to scan those which resulted in more resource usage. Sometimes somebody would manually delete the old branches but this is not a maintainable solution.
Solution: The Cleaning Crew
Just like any office building a regular cleaning crew comes in and cleans the office. We need something similar for our situation as well. Preferrably every night it should cleanup old branches.
Step 1: The Cleaning Script
Save this script to a path somewhere. In my situation this is C:\scripts\Git-CleanMergedRemoteBranches.ps1
.
Step 2: Run Script Nightly
To execute something periodically (every night at 11 PM in this example), the Task Scheduler in Windows is most suitable for this. Use the following script to add it as a new scheduled task:
You can view the newly added task via the Windows key -> type in “Task Scheduler” -> Hit ENTER. This brings up the task scheduler user interface and there you should see the task GitCleanergedRemoteBranches
. Right-click it and choose “Run” to do a test run. It should show a “… operation completed successfully (0x0)” result.