git worktrees step-by-step

last updated: Mar 05, 2024

https://infrequently.org/2021/07/worktrees-step-by-step/

I use worktrees, but without the bare repo setup described here. May want to look into using it with the gitdir: thing I had never heard of, instead of using the MAIN_BRANCH hack I've been using.

Docs on gitdir: are here and here and probably in some more places

Found via this piece, which has a companion on fetching remote branches which is apparently a shortcoming of the bare repository approach.


I tried to do this and I found the bare repository not to work very well for me at all - it does not want to list remote branches and just generally seems to be a pain to work with. I prefer the tooling I've built.


https://spin.atomicobject.com/2021/02/23/git-worktrees-untracked-files/

A neat hack! if you do cp -Rc node_modules ../some_branch/ on a mac, APFS will create a copy-on-write version of your node_modules directory; this is something I've wanted for a long time.

A copy-on-write (COW) clone duplicates an existing file without actually copying its contents on disk immediately. This is similar to a traditional hard link, except that if you modify a hard-linked file, the original file will be modified as well. But a COW clone copies the original if and only if it is written to (hence, “copy on write”).


I wrote up how I use worktrees in detail here: How I use git worktrees

↑ up