Cleaning up merged git branches: a one-liner from the CIA's leaked dev docs
Hacker News
February 20, 2026
AI-Generated Deep Dive Summary
Cleaning up merged Git branches can be a tedious task, but a simple one-liner from the CIA's leaked internal documents offers a高效 solution. In 2017, WikiLeaks published Vault7, which included a page of git tips and tricks. Among these was a command to delete all locally merged branches with a single line of code. This tip has proven invaluable for developers looking to streamline their workflow by eliminating outdated branches that clutter the branch list.
The problem arises when local repositories accumulate stale branches over time. After merging feature branches, hotfixes, or experiments into the main branch, these old branches remain unless manually deleted. Listing merged branches with `git branch --merged` reveals a graveyard of unnecessary branches. Deleting them one by one is not only time-consuming but also prone to errors. The CIA’s solution provides a cleaner and more efficient approach.
The original command works as follows: `git branch --merged | grep -v "\*\|master" | xargs -n 1 git branch -d`. Breaking it down, `git branch --merged` lists all local branches that have been merged into the current branch. The `grep -v "\*\|master"` filters out the current branch (denoted by an asterisk) and the master branch to prevent accidental deletion. Finally, `xargs -n 1 git branch -d` deletes each remaining branch safely, ensuring unmerged branches remain untouched.
For modern projects that use `main` instead of `
Verticals
techstartups
Originally published on Hacker News on 2/20/2026