Skip to content
项目

关于

git worktree, but copy-on-write on APFS, btrfs and XFS.

开发工具

这些数字如何统计

137 星标3 复刻 +1 24小时

实测星标增长

24小时
+1
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

git-coworktree

git coworktree is git worktree with copy-on-write checkouts on APFS, btrfs and XFS. Its add makes the new worktree share data blocks with an existing one, so another worktree of a large repository costs a fraction of the repository's size on disk. Every other subcommand is git worktree itself.

Install

go install go.dw1.io/git-coworktree@latest

Prebuilt binaries are on the Releases page.

Git runs any git-<name> program on PATH as git <name>, so $(go env GOPATH)/bin, or the directory you put a prebuilt git-coworktree in, must be on PATH.

Copy-on-write needs git 2.36 or later, and macOS (APFS) or a Linux filesystem with reflink support, such as btrfs or XFS. Elsewhere, Windows included, add runs plain git worktree add.

With a coding agent

Paste this into a coding agent to install git-coworktree:

Install git-coworktree, a copy-on-write drop-in for git worktree:
https://github.com/dwisiswant0/git-coworktree

1. If Go is installed, run `go install go.dw1.io/git-coworktree@latest`.
   Otherwise, download the archive that matches this machine from
   https://github.com/dwisiswant0/git-coworktree/releases, check it as its
   release notes describe, and extract `git-coworktree` into a directory.
2. Make sure the directory that holds `git-coworktree` is on PATH.
3. In this repository, check that `git coworktree list` prints the same as
   `git worktree list`.

Don't change any git config or hooks. Tell me where you installed it.

Optionally, also set up the agent's worktree hooks, so that the worktrees it creates on its own go through git coworktree too; see Coding agents.

Usage

Use it as you'd use git worktree:

git coworktree add -b feature ../feature
git coworktree list
git coworktree remove ../feature

add takes the options of git worktree add and prints the same output.

.worktreeinclude and include

A checkout leaves out ignored files such as .env or node_modules/. If the source worktree has a .worktreeinclude file at its root, add also places the ignored files it lists in the new worktree. It does so before the post-checkout hook runs, so a hook such as npm ci can use them. The file uses .gitignore syntax. An entry is placed only if it's both listed and ignored, so tracked files are never duplicated.

.env
node_modules/
config/local.json
  • Nothing is overwritten. An entry whose destination already exists is skipped, with one notice: line naming it.
  • An entry is also skipped when the new worktree lacks a directory above it that either worktree's index tracks, such as a directory outside a sparse checkout's cone.
  • Entries that hold other worktrees are left out.
  • Each entry is cloned where the filesystem can clone, and copied where it can't, with one notice: line that says how many entries and bytes were copied. Without copy-on-write, a large node_modules/ costs its full size. An entry keeps its permissions, modification times and extended attributes. A copy keeps extended attributes only where the volume stores them natively, such as APFS and HFS+. On FAT, exFAT and SMB volumes it leaves them out, rather than have macOS store them in ._* files. On Linux, only user.* attributes are kept. An attribute that the destination volume can't hold, because it's too large or the volume is full, is left out.
  • An entry appears under its real name only once it's complete, so an interrupted run leaves no partial entry.
  • A failure prints one warning: line and keeps the new worktree.

add applies .worktreeinclude only when it doesn't pass through to git. On Windows it always passes through, so it never applies .worktreeinclude.

git coworktree include <path> applies the source's .worktreeinclude to a worktree that already exists, such as one made by plain git worktree add. It's the one subcommand that git worktree doesn't have. A second run places only the entries that are still missing, which also finishes an interrupted one. include isn't supported on Windows: it prints an error and exits with status 1.

Configuration

coworktree.patchIndex is git-coworktree's only setting. It's true by default, and false makes add record the clones with git update-index --really-refresh instead of writing the index itself; Recording the clones explains the difference. Set it in a git config file, or for one run with git -c coworktree.patchIndex=false coworktree add.

Some of git's own settings make add clone less, and git writes the rest as usual:

  • Sparse checkout, core.autocrlf=true and core.eol=crlf turn cloning off for the whole worktree.
  • A file that the source marks assume-unchanged or skip-worktree isn't cloned. Under core.ignoreStat, git marks every file it checks out as assume-unchanged, so a source checked out that way gets almost nothing cloned.

Coding agents

The binary knows nothing about any agent. contrib/ has thin adapter scripts that map an agent's worktree hooks onto git coworktree.

AGENTS.md

To have agents use it, add this to the repository's AGENTS.md:

## Worktrees

- Create worktrees with `git coworktree add` instead of `git worktree add`.
  It takes the same options and prints the same output, and where the
  filesystem supports it, the new worktree shares disk blocks with this one.
- `git coworktree add` also places the ignored files that `.worktreeinclude`
  lists, such as `.env`, in the new worktree. Don't copy them by hand.
- For a worktree made another way, run `git coworktree include <path>` to
  place them.

Don't create a worktree:

- for a question, a one-file fix, or anything that finishes within the
  current task; work in the current checkout;
- to check out a branch that another worktree already has; create a new
  branch, or use `--detach`, and never `--force`;
- when the disk is nearly full; on a filesystem without copy-on-write, each
  worktree and its `.worktreeinclude` entries cost their full size.

How add saves disk

  1. Git creates the worktree without checking it out, locked as initializing so that git worktree prune leaves it alone.
  2. git-coworktree clones every tracked file that's unmodified in the source worktree into the new one. On APFS it clones a whole directory with one call when everything in it qualifies by size, mode and BSD flags, and single files elsewhere. On Linux it clones single files.
  3. git-coworktree hashes each clone in parallel and records the ones that match the commit in the index that git wrote with git read-tree. Git's git update-index --really-refresh does this where the record can't be made; see Recording the clones.
  4. git reset --hard, the step git worktree add runs internally, writes whatever is missing or wrong.

The files, index, ORIG_HEAD, reflog and output all come from git. Cloning only decides how many files share blocks with the source. It never copies untracked files or uncommitted changes. If cloning hits an error, add prints one warning: line and git writes the remaining files.

The source is the worktree you run the command in. Outside a worktree, such as in a bare repository, it's a linked worktree at the same commit as the new one, or else the first one git worktree list shows.

A clone is kept only if checkout would write exactly its bytes and metadata. Git writes the files it converts on checkout: anything with a filter (including Git LFS), ident, working-tree-encoding, or CRLF line endings. It also writes symlinks, submodules, assume-unchanged and skip-worktree entries, and files whose mode, extended attributes or BSD flags differ from what git would create.

add runs plain git worktree add when:

  • --no-checkout or --orphan is in effect, including the --orphan that git infers in a repository without commits;
  • an option is abbreviated, such as --det, or isn't one that git worktree add 2.50 has;
  • GIT_INDEX_FILE is set;
  • git is older than 2.36.

It makes no clones when the filesystem can't clone, when the new worktree is on a different filesystem from the source, or when the new worktree uses sparse checkout. Git then checks out every file, as usual.

Recording the clones

git update-index --really-refresh hashes every clone on one core, and each clone is a new file that nothing has cached. git-coworktree does the hashing itself, with up to 8 workers, and writes the result into the index. For each clone it opens the file without following a symlink, streams as many bytes as fstat reports for the open file, and hashes them as a git blob with SHA-1 collision detection, or with SHA-256 in a SHA-256 repository. It stats the file before and after, and accepts it only if the hash is the index's object ID, the stat didn't change, and the file has the mode, BSD flags and extended attributes that checkout would give it. Then it overwrites the ctime, mtime, device, inode, owner and size fields of those entries in the index, sets their assume-valid bit if core.ignoreStat is true, as git's checkout does, and leaves every other byte as git wrote it. It writes through index.lock and renames the result over index, as git does. A clone that fails any of this keeps an empty stat, and git reset --hard writes the file again.

After the write, it runs two checks. It reads the file back and checks that it holds the bytes it wrote and that those bytes carry their trailer, which git verifies only in git fsck. Then it has git read the file with git ls-files -s --debug -z and compares every path, mode, object ID, stage and flags, and every stat field, with what it meant to write: the recorded stat and flags for the entries it patched, and for every other entry what git read-tree wrote. Git's parser is independent of the one that wrote the file, so it catches a layout mistake that the read-back shares. If git's output doesn't parse as expected, the check fails. If either check fails, it reads the commit into the index again and runs git update-index --really-refresh.

add skips the patch, without a message, and runs git update-index --really-refresh when:

  • coworktree.patchIndex is false;
  • the index has a required extension, such as link (split index) or sdir (sparse index), or a version or object format that git-coworktree doesn't know;
  • index.lock exists.

Whenever the refresh records the clones, add first removes every clone that isn't the file checkout would create: the refresh looks at no more of a file's mode than the exec bit, and not at its BSD flags or extended attributes.

Set coworktree.patchIndex=false in a git config file to always use git update-index --really-refresh, or pass git -c coworktree.patchIndex=false coworktree add for one run. Hashing every clone stays the default either way. The final index is the one that git reset --hard writes, as for native add.

Disk and time

I measured git coworktree add against git worktree add on an Apple M5 Pro, on APFS under macOS 26.7.3 with git 2.50.1, and with a warm page cache. Times are medians of 10 runs after a warmup. Disk is how much free space df reports gone after one add, over two runs. du counts a clone as a full copy, so it can't measure this.

Repository (tracked files) New worktree's commit git worktree add git coworktree add Disk used
next.js (35,510) The source's 4.57 s 3.15 s (0.69×) 298 MiB → 21–22 MiB (93% less)
next.js HEAD~200 4.56 s 3.65 s (0.80×) 297–298 MiB → 71–72 MiB (76% less)
linux v6.12 (86,680) The source's 10.59 s 4.73 s (0.45×) 1,655–1,656 MiB → 39 MiB (98% less)
rust (64,958) The source's 6.90 s 3.57 s (0.52×) 459 MiB → 31–33 MiB (93% less)
rust HEAD~200 6.73 s 7.29 s (1.08×) 449 MiB → 149–150 MiB (67% less)

HEAD~200 follows first parents: it's 200 commits back on next.js, but 6,444 on rust, which merges. The load average was 1.8 when the runs started and 3.3 when they ended, and standard deviations were 0.07 to 0.17 s, except 0.90 s for native add of next.js, from one run of 7.42 s.

Native add writes every file. git-coworktree clones them instead, which writes no file data, and hashes the clones on up to 8 workers. In three traced adds of linux, git-coworktree's own work, mostly cloning and hashing, took 4.0 to 5.2 s, and git's check of the written index 0.08 s. The savings shrink as the new worktree's commit diverges from the source's, because only files that are the same in both get cloned. Whole-directory clones also need every file in a directory to match. They held 99.9% of the cloned files on rust at the source's commit, and 24% at HEAD~200.

On Linux, I measured the same way on XFS with reflink=1, at the same commits, on a CI runner with 4 vCPUs (AMD EPYC 9455), kernel 6.14 and git 2.55.0. The file system was on a loop-mounted image. Standard deviations were 0.06 to 0.86 s, and both disk runs gave the same figure.

Repository (tracked files) New worktree's commit git worktree add git coworktree add Disk used
next.js (35,510) The source's 1.05 s 1.24 s (1.18×) 304 MiB → 34 MiB (89% less)
next.js HEAD~200 1.00 s 1.21 s (1.21×) 304 MiB → 83 MiB (73% less)
linux v6.12 (86,680) The source's 4.96 s 3.00 s (0.61×) 1,667 MiB → 63 MiB (96% less)
rust (64,958) The source's 1.63 s 1.87 s (1.15×) 466 MiB → 48 MiB (90% less)
rust HEAD~200 1.62 s 2.27 s (1.40×) 456 MiB → 162 MiB (64% less)

On XFS, add takes 1.15 to 1.4 times as long as native on next.js and rust, and less on linux. Linux can't clone a whole directory, so each file gets a clone of its own, and native add writes next.js and rust in 1.0 to 1.6 s on this runner.

Differences from git worktree add

On Windows, add is native add, so none of these differences apply.

  • add can take more or less time than native add; see Disk and time.
  • The post-checkout hook runs through git hook run. It sees GIT_DIR, set to the new worktree's admin directory, which native add unsets, and an empty GIT_PREFIX where native add passes the subdirectory you ran it from. When core.hooksPath is relative, add passes the hooks directory that git would use with -c, so the hook also sees it in GIT_CONFIG_PARAMETERS.
  • When add can't remove its initializing lock, it prints warning: could not unlock the new worktree: and git's error, where native add prints unable to unlink '<admin dir>/locked' and the system's error. Both carry on with the post-checkout hook and keep the exit status, and add still applies .worktreeinclude. A lock that a hook deleted first is no failure in either, and neither prints anything.
  • Native add runs the post-index-change hook once. add runs it twice when it records the clones by patching the index: after read-tree puts the commit's entries in the index, and after reset --hard, which is the one native add also makes. When it falls back to git update-index --really-refresh, for example with coworktree.patchIndex=false, the refresh adds a third run, and a failed check of the patched index adds a fourth, because the commit is read into the index again. With no file cloned there's nothing to record, so the hook runs twice. When add doesn't seed, such as under core.autocrlf=true, it runs once. I counted these with a logging hook on git 2.50.1.
  • Like native add, a SIGINT, SIGHUP, SIGTERM, SIGQUIT or SIGPIPE before the checkout finishes removes the new worktree. add removes only a worktree that its own git worktree add reported making, so a signal that arrives while git creates the worktree takes effect once git has finished. A second signal goes to that git and stops it, for example when a hook hangs; git's own cleanup then removes what it made, and add removes nothing. After SIGQUIT or SIGPIPE, add exits with status 128 plus the signal number instead of dying from the signal; a shell reports both the same way. On macOS, a SIGPIPE sent with kill while files are cloned doesn't stop add, because the Go runtime doesn't deliver it.
  • A SIGKILL leaves a worktree locked as initializing, as it does for native add. If a source file was being edited while it was cloned, that worktree can hold the uncommitted content. A signal sent to the whole process group, such as Control+C in a terminal, or a second signal sent to add, can leave an empty one in a rare case: when it reaches git just after git made the worktree. git worktree remove --force --force <path> removes either.
  • On APFS, two tracked names that differ only in Unicode normalization share one file, and the one left on disk can differ from native.
  • A clone keeps the source file's modification time, while a file that git writes gets the current time, so a new worktree can hold both.
  • On macOS, if a tracked file in the source gets the uchg flag while add clones its directory, the clone carries the flag, and git reset --hard can't replace it. add fails with error: unable to unlink old '<file>': Operation not permitted and fatal: Could not reset index file to revision 'HEAD'., and leaves the new directory behind. It isn't a worktree any more, so git worktree remove refuses it, and rm -rf can't delete the locked file. Run chflags -R nouchg <path>, then rm -rf <path>.
  • On macOS, if a tracked directory in the source becomes read-only while add clones it, its clone is read-only too. When add patches the index, it fails with error: unable to unlink old '<file>': Permission denied for each file in that directory and fatal: Could not reset index file to revision 'HEAD'., and leaves the new directory behind. It isn't a worktree any more, so git worktree remove refuses it. When git's refresh records the clones, add succeeds after a warning: that seeding stopped, and the directory keeps mode 0555 where git gives 0755. git worktree remove --force --force then fails with Permission denied. Either way, run chmod -R u+w <path>, then rm -rf <path>.
  • include isn't a git worktree subcommand. If git ever adds one, git coworktree include still runs this one.

License

Apache 2.0.

Copyright (C) 2026 Dwi Siswanto.

See LICENSE.

探索相关项目

加载中…