展示HN:Nit – 我用Zig重构了Git,为AI代理节省了71%的token。
Show HN: Nit – I rebuilt Git in Zig to save AI agents 71% on tokens

原始链接: https://justfielding.com/blog/nit-replacing-git-with-zig

AI 代理经常与 Git 交互,消耗了大量 shell 命令 token,占比高达 7.4%(对于 Codex 超过 10%)。这是因为 Git 的人类可读输出冗长,包含机器不需要的额外信息。为了解决这个问题,作者创建了 **nit**,一个用 Zig 编写的 Git 替代品,它通过 `libgit2` 直接访问 Git 对象数据库。 Nit 优先考虑机器使用,提供显著的 token 节省——`log` 命令最多节省 87%,`diff` 命令最多节省 35%——并提高了速度(`status` 命令最快可提升 1.64 倍)。一个关键的优化是将 diff 上下文减少到一行(U1),出乎意料的是,测试表明这并没有影响 Claude 的理解能力。 Nit 有两种模式:紧凑的、机器优化的默认模式和人类可读模式(`-H`)。它采用直通设计,对于不支持的命令会回退到标准的 Git,从而确保完整的功能并允许逐步优化。可以通过 `brew install fielding/tap/nit` 安装,并可以安全地将其别名为 `git=nit`。

一位开发者在Hacker News上用Zig编程语言重构了Git,名为“Nit”,旨在将AI代理的token使用量减少71%。该项目最初看起来是完全重写,但实际上对于未自定义实现的功能,它作为标准Git命令的包装器,允许代理回退到熟悉的工具。 讨论集中在Git输出是否真的是AI代理的瓶颈,以及重写是否比简单的包装器更有必要。一些评论员指出已经存在类似目标的工具,例如`rtk`。 然而,该项目也受到了批评,一位评论员认为它不太可能被视为一个严肃的项目,并警告不要虚假声称LLM辅助的能力,尤其是在文章自身生成的LLM内容方面。
相关文章

原文

AI agents call git constantly. Status, diff, log, show. I pulled data from 3,156 real coding sessions and git accounted for roughly 459,000 tokens of output. That’s 7.4% of all shell commands. Codex is even worse (over 10% of its bash calls are git).

Makes sense though right? git’s output was designed for humans. Verbose headers, instructional text, column padding, decorative formatting. It’s the informational equivalent of wrapping every answer in a gift bag with tissue paper. Machines don’t need the tissue paper or the gift bag. Every extra token costs money and adds latency.

So I built nit. A native git replacement written in Zig that talks directly to the git object database via libgit2. Defaults tuned for machines.

The Numbers

Token savings (nit compact vs git default):

Commandgit tokensnit tokensSavings
status~125~3671%
log -20~2,273~30187%
diff~1,016~65735%
show —stat~260~11855%

Across real session data, nit’s compact defaults would save 150-250K tokens. That’s something… oh, and did I mention it’s faster?

100 hyperfine runs on a real repo:

CommandgitnitSpeedup
status13.7ms8.4ms1.64x
diff14.3ms9.9ms1.44x
show10.2ms7.3ms1.39x

How It Works

Zig’s C interop is zero-cost. You @cImport the libgit2 headers and call functions directly. No subprocess overhead, no text parsing. nit reads the git object database natively.

For commands nit hasn’t optimized yet, it falls through to git via execvpe(), replacing the nit process entirely. Zero wrapper overhead. This makes alias git=nit safe. You never lose functionality, and as more commands get native implementations, the passthrough shrinks on its own.

The U1 Experiment

This was the most controversial design decision. I reduced diff context from 3 lines (git’s default) to 1 line. There are a lot of tokens hiding in those extra context lines. But does cutting them actually hurt comprehension?

I ran 27 trials with multi-file diffs, nested control flow, code moves, ambiguous similar blocks. Claude scored 4/4 at U0, U1, and U3. No difference at all. So, why not U0 then?

I checked real-world behavior. Across 561 git diff/show calls from actual Claude Code sessions, only 3.9% of agents read the source file immediately after diffing. This suggests that the diff itself is a primary source of the agent’s surrounding context, so I settled.

U1 gives you the savings without the downside. Hunk headers still have line numbers. The changed lines speak for themselves.

Two Modes

Compact (default): Machine-optimized. Just the data.

Human (-H): Colored, grouped. For when you’re the one reading it.

nit status compact vs human comparison

What I Learned

The hardest part wasn’t performance. It was conformance. Git has decades of edge cases: detached HEAD, merge commits, renamed files, binary diffs, submodules. I wrote 78 conformance tests covering all of them. Every time nit’s output diverged from git’s in a meaningful way, I added a test and fixed it.

The passthrough design made this manageable. I didn’t need to implement everything on day one. Start with the highest-impact commands (status, diff, log, show). Let git handle the rest. Ship it, iterate.

Install it

brew install fielding/tap/nit

Try it

if you’re an agent:

nit log

if you’re a human:

nit log -H

Forget about it

alias git=nit

Everything you don’t notice is git. Everything that’s faster is nit.

联系我们 contact @ memedata.com