我从泄露的中央情报局开发者文档中找到了一条有用的Git单行命令。
I found a useful Git one liner buried in leaked CIA developer docs

原始链接: https://spencer.wtf/2026/02/20/cleaning-up-merged-git-branches-a-one-liner-from-the-cias-leaked-dev-docs.html

这篇文章介绍了一个有用的Git命令,最初在泄露的CIA文件(Vault7,2017)中发现。该命令有效地清理本地Git仓库,删除已合并的分支,防止`git branch`列表变得混乱。 核心命令`git branch --merged | grep -v "\*\|master" | xargs -n 1 git branch -d`列出已合并的分支,排除当前分支和“master”,然后安全地删除它们。 作者建议为使用“main”作为主分支的现代项目更新此命令:`git branch --merged origin/main | grep -vE "^\s*(\*|main|develop)" | xargs -n 1 git branch -d`。 他们甚至创建了一个Git别名“ciaclean”,方便使用。这个简单的工具通过在部署后快速将潜在的冗长分支列表减少到可管理的几个,从而节省时间和保持组织性。

一场由泄露的CIA文件中发现的一条Git单行命令引发的Hacker News讨论,围绕着高效清理本地Git分支展开。原始帖子分享了一个shell脚本,用于切换到`main`,修剪远程跟踪分支,删除所有本地分支*除了*`main`,然后拉取最新的更改——这是一个故意具有破坏性的操作。 评论者提供了替代方案和相关工具。一些人指出,`git fetch -p` 可以与集中式Git主机自动实现类似的修剪。 另一些人讨论了使用Claude构建基于Textual的TUI(文本用户界面)来进行Git工作流管理,但这种方法因计算成本高昂而受到批评。 现有的解决方案,如`git drop-merged`脚本和oh-my-zsh框架中的别名(`gbda`,`gbds`)也被强调,表明该功能并非全新,但仍然有用。 最近,Fork.app Git客户端在Mac上提供类似的分支清理功能。
相关文章

原文

In 2017, WikiLeaks published Vault7 - a large cache of CIA hacking tools and internal documents. Buried among the exploits and surveillance tools was something far more mundane: a page of internal developer documentation with git tips and tricks.

Most of it is fairly standard stuff, amending commits, stashing changes, using bisect. But one tip has lived in my ~/.zshrc ever since.

The Problem

Over time, a local git repo accumulates stale branches. Every feature branch, hotfix, and experiment you’ve ever merged sits there doing nothing. git branch starts to look like a graveyard.

You can list merged branches with:

git branch --merged

But deleting them one by one is tedious. The CIA’s dev team has a cleaner solution:

The original command

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 out the current branch (*) and master so you don’t delete either
  • xargs -n 1 git branch -d — deletes each remaining branch one at a time, safely (lowercase -d won’t touch unmerged branches)

The updated command

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

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

Run this from main after a deployment and your branch list goes from 40 entries back down to a handful.

I keep this 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

Small thing, but one of those commands that quietly saves a few minutes every week and keeps me organised.

Thanks for reading

You can follow me here for my latest thoughts and projects

联系我们 contact @ memedata.com