Guide · Rust · git worktrees
How to share a Cargo build cache between git worktrees
Each git worktree of a Rust project has its own target/, so Cargo compiles every dependency again in each one. That is the price of a second branch, a review checkout or a coding agent in its own worktree. Here are the three usual ways to share that work, what each one shares, and where each one stops.
Why every worktree builds from zero
Cargo keeps its build cache in the workspace's target/ directory. A new worktree is a new directory with an empty target/, so the first build there compiles serde, syn, tokio and the rest again, and stores another copy of each on disk. The same happens for a second clone, and for a branch switch that changes Cargo.lock and back.
git worktree add ../myproject-review main
cd ../myproject-review
cargo build # compiles every dependency again
Option 2: sccache
cargo install sccache --locked
export RUSTC_WRAPPER=sccache
sccache wraps each rustc call and keeps compiled results in a cache outside target/, locally or in remote storage, so worktrees and CI machines can share them.
- It sees one
rustccall at a time. Its key depends on absolute paths unless they are normalized, so the same crate compiled from two checkout paths can miss. - Crates that invoke the linker (
bin,proc-macro,dylib) are not cached. - Cargo still plans every build and runs every build script in each worktree.
- It is the mature choice when you need a cache shared between machines.
Option 3: Mitts, a machine-wide store
Mitts reads the same Cargo.toml and Cargo.lock, plans the build itself and runs rustc directly. Every unit gets a key from all of its inputs (toolchain, target, profile, flags, features, source contents, dependency keys, tracked environment). The output is stored once per machine, read-only, and restored into every worktree that needs the same key. An input it cannot account for means a rebuild, not a guess.
curl -fsSL https://mittsbuild.com/install.sh | sh # Windows: irm https://mittsbuild.com/install.ps1 | iex
cd myproject && mitts build
git worktree add ../myproject-review HEAD
cd ../myproject-review && mitts build # dependencies come from the store
- Shared: registry and git dependencies, including build-script runs, proc macros and linked units, whenever their keys match. Builds in several worktrees at once take per-key locks, so a dependency two of them need is compiled once.
- Not shared by default: your own crates (workspace members and path dependencies). They stay in each worktree's
target/: an unchanged rebuild restores them, an edit replaces them, a second worktree compiles them again.MITTS_SHARE_WORKSPACE_CRATES=1shares them too, when their key can be made path-independent (noCARGO_MANIFEST_DIRread, no build script, no proc macro declared impure). - Never cached:
cdylib,dylibandstaticlibcrates, and crates whoseenv!reads cannot be tracked.mitts explainlists every unit and why. - Per machine only. There is no remote or team cache yet.
- Not a sandbox. A build script or proc macro that reads files it does not declare can give a stale result, as with Cargo.
- Pre-release. Mitts is young software; Cargo, optionally with sccache, is the safe choice where you need a stable release.
One run, shown as it came out
ripgrep 14.1.1, dev profile, on Mitts 0.5.9 and rustc 1.99.1, an Apple M2 Ultra (24 cores, macOS 15.7.8, APFS), with an empty Mitts cache and sources fetched beforehand. One run on a desktop that was not otherwise idle: it shows what happens, it is not a benchmark.
| Step | Wall time | Units |
|---|---|---|
| First build, empty cache | 6.5 s | 33 compiled |
| Same build again, nothing changed | 0.016 s | nothing to do |
Second worktree, same commit, empty target/ | 5.5 s | 9 compiled, 24 from the store |
The 24 restored units are ripgrep's dependencies. The 9 compiled ones are ripgrep's own crates, which a second worktree builds again by default. ripgrep's own crates are most of its build, so here the second worktree saves little; the more of a build goes into dependencies, the more a second worktree saves.
Repeat it:
git clone --depth 1 --branch 14.1.1 https://github.com/BurntSushi/ripgrep.git rg-a
cd rg-a
export MITTS_CACHE_DIR=$PWD/../cache # a fresh, empty cache
mitts fetch
mitts build # first build
mitts build # nothing changed
git worktree add --detach ../rg-b HEAD && cd ../rg-b
mitts build # second worktree
For many projects and parallel worktrees, measured against Cargo and sccache on three machines, see The Hundred. It was measured with Mitts 0.3.17, which also shared the projects' own crates by default.
Which one to use
- One worktree at a time, no extra tools: a shared
CARGO_TARGET_DIR, accepting the lock and the shared binaries. - A cache shared between machines or with CI: sccache.
- Several worktrees building on one machine at the same time, for branches, reviews or coding agents: Mitts keeps each worktree's
target/separate and shares the dependencies between them.
More: what a second checkout reuses, what decides a cache hit, and the source on GitHub.