Manage Git Branches with git-trim

Developer Tools

February 11th, 2022

Manage Git Branches with git-trim

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. 🗑😎https://t.co/fW4yTkPSJt

— 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:

# Removes local branches where its remote branch no longer exists
git trim
git trim --pruned
 
# Removes local branches already merged into the current branch
git trim --merged
 
# Removes local branches without commits in the last 3 months
git trim --stale
 
# Removes local branches not tracking a remote branch
git trim --untracked
 
# Removes all local branches except the current branch (requires confirmation)
git trim --all

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

git-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.