Manage Git Branches with git-trim

Developer Tools

February 11th, 2022


Have you ever needed to clean up stale or old local branches that have already been merged and deleted on GitHub? Jason McCreary has released a CLI tool for organizing local and remote branches:

Cleaning up thousands of dangling references in Git repos everywhere - another "Garbage Man" production. 🗑😎

— Jason McCreary 🗑 (@gonedark) February 9, 2022

The git-trim command is a way to quickly remove merged, pruned, untracked, and stale branches in Git repositories:

1# Removes local branches where its remote branch no longer exists
2git trim
3git trim --pruned
5# Removes local branches already merged into the current branch
6git trim --merged
8# Removes local branches without commits in the last 3 months
9git trim --stale
11# Removes local branches not tracking a remote branch
12git trim --untracked
14# Removes all local branches except the current branch (requires confirmation)
15git trim --all

You can also combine these when it makes sense, such as removing merged and stale branches:

1git-trim --merged --stale

You can download and try this command out via NPM or as an Oh-My-Zsh plugin. If you'd like to learn more about the source code, you can check it out on GitHub at jasonmccreary/git-trim.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners