As a developer frequently juggling multiple repositories, I often find myself distracted by the persistent urge to delete old, outdated Git branches after they have been merged. This compulsion tends to slow me down, especially since I work across both personal and professional projects. I needed a solution that could streamline this process across all my repositories and save me valuable time. Additionally, I realized it had been a while since I last practiced writing shell scripts, making this the perfect opportunity to refresh my skills and create a versatile tool. With these goals in mind, I developed MrGitClean! 🧼
MrGitClean is a shell-based utility I developed to simplify managing Git branches. If you’ve ever worked on a large team where dozens of feature branches are merged weekly, you know how quickly things can spiral out of control. Without regular cleanup, repositories become cluttered, leading to confusion and slower Git operations. I designed MrGitClean to solve this problem. Here’s what it does:
I wanted a tool that wouldn’t just clean branches but would make the process safe, efficient, and easy to use.
When designing MrGitClean, ensuring user safety was my foremost concern. The tool is programmed to prompt you for confirmation before deleting any branch, keeping you firmly in control of the process. Additionally, it offers insightful feedback to assist you, even if you’re new to Git, ensuring a smooth and intuitive experience.
➜ mrgitclean
Default branch is: main
Found 4 merged branches eligible for cleanup.
-----------------------------------------
Branch: feature/add-new-auth-system
Last commit: 2024-12-09 16:32:23 -0500 (6 day(s) ago)
Merge commit: 18930fa73094e1222cc89960f1ea3c89547173e5
Delete this branch? [y/N] n
Skipped 'feature/add-new-auth-system'
Manually cleaning branches one by one is tedious, especially with large repositories. That’s why I added batch mode for automated cleanup. This mode is ideal for CI/CD pipelines or teams managing repositories with hundreds of branches.
$ ./mrgitclean --batch --remote --exclude="release/*"
Deleted branches:
- feature/add-auth
- bugfix/fix-typo
Batch mode has been a lifesaver for my own projects, and I’m confident it’ll save you time too.
To avoid accidental deletions, MrGitClean includes a dry-run mode. This feature lets you see exactly what the tool would delete before it takes action, so you can proceed with confidence.
➜ mrgitclean --dry-run
Default branch is: main
Found 2 merged branches eligible for cleanup.
-----------------------------------------
Branch: feature/add-new-auth-system
Last commit: 2024-12-09 16:32:23 -0500 (6 day(s) ago)
Merge commit: 18930fa73094e1222cc89960f1ea3c89547173e5
[DRY-RUN] Would delete: feature/add-new-auth-system
This feature has been invaluable in providing peace of mind to my workflow, especially when managing critical repositories.
I wanted MrGitClean to be a comprehensive solution, so I added support for cleaning up remote branches as well. This is especially helpful when collaborating with teams, as it prevents remote clutter from accumulating.
➜ mrgitclean --dry-run --batch --remote
Default branch is: main
Found 2 merged branches eligible for cleanup.
-----------------------------------------
Warning: Deleting remote branches affects all users.
Branch: feature/add-new-auth-system
Last commit: 2024-12-09 16:32:23 -0500 (6 day(s) ago)
Merge commit: 18930fa73094e1222cc89960f1ea3c89547173e5
[DRY-RUN] Would delete: feature/add-new-auth-system
[DRY-RUN] Would delete remote branch 'feature/add-new-auth-system'
I chose to write MrGitClean as a shell script because of its flexibility and cross-platform compatibility. Zsh and Bash are widely available on Unix-based systems like macOS and Linux, making the tool easy to run in diverse environments. Writing it in a shell also allowed me to keep dependencies minimal and execution fast, which was a priority for me.
Another reason for choosing shell scripting was my goal of eventually making MrGitClean available through Homebrew. This would make installation and updates seamless for users, further enhancing the tool’s accessibility.
Even though I’ve already made MrGitClean a robust tool, there’s always room for improvement. Here are some enhancements I’m planning:
If you have ideas or suggestions for additional features, I’d love to hear them! Feel free to share your thoughts on GitHub or in the comments section.
If you’re ready to simplify your Git branch management, check it out on GitHub!
I’m excited to see how it helps others, and I welcome any feedback or feature requests to make it even better.