Cleaning Git repos

I guess, all popular Git repositories allow you to delete source branch when the PR with this branch is accepted. But sometimes you forget to use this option and end up with hundreds or thousands of stale branches across dozens of projects.


gitclean to the resque!

Prepare a list of repositories that you want to clean up. It does not matter what Git service is used (GitHub, GitLab, Bitbucket or something else), all you need is push access. List should look like this, one repo URL per line:

[email protected]:madhead/
[email protected]:madhead/mentoring.git

Now clone this repo and build it (./gradlew assemble). Unzip or untar a resulting distribution and run the tool with a single argument — path to the file with a list of repos:

./bin/gitclean /home/madhead/Downloads/repos.list

gitclean will iterate through that list, clone each repo in a temporary folder and delete branches that were already merged into master (basically, branches from git branch --remotes --merged master).

Bonus tips

Here is some shell magic that will do the same thing for a single repo:

git branch --remotes --merged master | grep -v master | cut -d/ -f2- | xargs -n 1 git push --delete origin
git branch --merged master | grep -v master | xargs -n 1 git branch -d
comments powered by Disqus