dwisiswant0/git-coworktree
حول
git worktree, but copy-on-write on APFS, btrfs and XFS.
أدوات التطويرالنمو المقاس للنجوم
- خلال 24 ساعة
- +١
- خلال 7 أيام
- جارٍ جمع السجل
- خلال 15 يومًا
- جارٍ جمع السجل
- خلال شهر
- جارٍ جمع السجل
تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.
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 largenode_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, onlyuser.*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=trueandcore.eol=crlfturn 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
- Git creates the worktree without checking it out, locked as
initializingso thatgit worktree pruneleaves it alone. - 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.
- 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'sgit update-index --really-refreshdoes this where the record can't be made; see Recording the clones. git reset --hard, the stepgit worktree addruns 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-checkoutor--orphanis in effect, including the--orphanthat git infers in a repository without commits;- an option is abbreviated, such as
--det, or isn't one thatgit worktree add2.50 has; GIT_INDEX_FILEis 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.patchIndexisfalse;- the index has a required extension, such as
link(split index) orsdir(sparse index), or a version or object format that git-coworktree doesn't know; index.lockexists.
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.
addcan take more or less time than nativeadd; see Disk and time.- The
post-checkouthook runs throughgit hook run. It seesGIT_DIR, set to the new worktree's admin directory, which nativeaddunsets, and an emptyGIT_PREFIXwhere nativeaddpasses the subdirectory you ran it from. Whencore.hooksPathis relative,addpasses the hooks directory that git would use with-c, so the hook also sees it inGIT_CONFIG_PARAMETERS. - When
addcan't remove itsinitializinglock, it printswarning: could not unlock the new worktree:and git's error, where nativeaddprintsunable to unlink '<admin dir>/locked'and the system's error. Both carry on with thepost-checkouthook and keep the exit status, andaddstill applies.worktreeinclude. A lock that a hook deleted first is no failure in either, and neither prints anything. - Native
addruns thepost-index-changehook once.addruns it twice when it records the clones by patching the index: afterread-treeputs the commit's entries in the index, and afterreset --hard, which is the one nativeaddalso makes. When it falls back togit update-index --really-refresh, for example withcoworktree.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. Whenadddoesn't seed, such as undercore.autocrlf=true, it runs once. I counted these with a logging hook on git 2.50.1. - Like native
add, aSIGINT,SIGHUP,SIGTERM,SIGQUITorSIGPIPEbefore the checkout finishes removes the new worktree.addremoves only a worktree that its owngit worktree addreported 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, andaddremoves nothing. AfterSIGQUITorSIGPIPE,addexits with status 128 plus the signal number instead of dying from the signal; a shell reports both the same way. On macOS, aSIGPIPEsent withkillwhile files are cloned doesn't stopadd, because the Go runtime doesn't deliver it. - A
SIGKILLleaves a worktree locked asinitializing, as it does for nativeadd. 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 toadd, 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
uchgflag whileaddclones its directory, the clone carries the flag, andgit reset --hardcan't replace it.addfails witherror: unable to unlink old '<file>': Operation not permittedandfatal: Could not reset index file to revision 'HEAD'., and leaves the new directory behind. It isn't a worktree any more, sogit worktree removerefuses it, andrm -rfcan't delete the locked file. Runchflags -R nouchg <path>, thenrm -rf <path>. - On macOS, if a tracked directory in the source becomes read-only while
addclones it, its clone is read-only too. Whenaddpatches the index, it fails witherror: unable to unlink old '<file>': Permission deniedfor each file in that directory andfatal: Could not reset index file to revision 'HEAD'., and leaves the new directory behind. It isn't a worktree any more, sogit worktree removerefuses it. When git's refresh records the clones,addsucceeds after awarning:that seeding stopped, and the directory keeps mode 0555 where git gives 0755.git worktree remove --force --forcethen fails withPermission denied. Either way, runchmod -R u+w <path>, thenrm -rf <path>. includeisn't agit worktreesubcommand. If git ever adds one,git coworktree includestill runs this one.
License
Apache 2.0.
Copyright (C) 2026 Dwi Siswanto.
See LICENSE.
استكشف مشاريع مشابهة ومرتبطة
جارٍ التحميل…