Cleaning up merged git branches: a one-liner from the CIA’s leaked dev docs




In 2017, WikiLeaks published Vault7 – a large repository of CIA hacking tools and internal documents. Buried among the exploits and monitoring tools was something more mundane: a page of internal developer documentation with Git tips and tricks.

Most of it is fairly standard stuff, modifying commits, hiding changes, using bisect. But a trick is alive inside me ~/.zshrc since.

Problem

Over time, a local Git repo accumulates old branches. Every feature branch, hotfix, and experiment you’ve merged sits there doing nothing. git branch Looks like a graveyard.

You can list merged branches with:

git branch --merged

But it is difficult to remove them one by one. The CIA development team has a neat solution:

original order

git branch --merged | grep -v "\*\|master" | xargs -n 1 git branch -d

how it works:

  • git branch --merged – Lists all local branches that have already been merged into the current branch
  • grep -v "\*\|master" – Filters the current branch (*) And master That’s why you also don’t delete it
  • xargs -n 1 git branch -d – Safely deletes each remaining branch at once (lowercase). -d Will not touch unconnected branches)

update order

Since most projects now use main instead of masterYou can update the command and exclude other branches you use frequently:

git branch --merged origin/main | grep -vE "^\s*(\*|main|develop)" | xargs -n 1 git branch -d

run it from here main One deploy later and your branch list reduces from 40 entries to a handful.

I keep it as a git alias so I don’t have to remember the syntax:

alias ciaclean='git branch --merged origin/main | grep -vE "^\s*(\*|main|develop)" | xargs -n 1 git branch -d'

Then in your repo just run:

ciaclean

A small thing, but one of those orders that quietly saves me a few minutes every week and keeps me organized.

thanks for reading

You can follow me for my latest ideas and projects here





<a href

Leave a Comment