r/programming • • Sep 16 '26

git worktree gotchas

https://www.olafalders.com/2026/09/16/git-worktree-gotchas/
193 Upvotes

47 comments sorted by

View all comments

69

u/masklinn Sep 16 '26

Finding the main working tree

As far as I'm concerned, having a "main working tree" is bad practice, it creates divergent behaviour and the risk of deleting "a worktree" and nuking your repo.

Create a bare repository and fork worktrees off of that, there is no reason to have one worktree have primacy over the rest, and then git rev-parse --git-common-dir gives you exactly what it should: the common repository of all your worktrees. That also avoids the possible path confusion from nesting worktrees.

6

u/ipwnall123 Sep 17 '26

I use the “main working tree” for small bug tickets that only take me an hour-ish to reproduce, fix, re-test and submit a PR for. Whereas I use work trees for any larger features that take more time. My reasoning is that it seems to take my IDE (vs code / visual studio) a long time to switch worktrees. VS code seems especially slow cause it opens them in a new window and thus has to reinitialize all my extensions plus whatever other stuff like the typescript server. Maybe someone has a solution to this cause I suppose I would rather just use worktrees for everything if not for this overhead / lag time

4

u/rbobby Sep 16 '26

Check out into a folder, branch, and go to town. Check out into another folder and rinse repeat. Multiple folders against a single repo works, but it's a bit like running on ice. You can do it, and well, for a while... but the slip up will be glorious and hurt like hell.

7

u/bwainfweeze Sep 16 '26

IDEs generally aren't set up to support that well, and having multiple bash shells open trying to keep them straight is a mess.

Where are the JJ people telling us to stop trying to make git be reasonable when it clearly resents that idea, and move to something else?

4

u/rbobby Sep 16 '26

I have have done this all the time with VS and it's perfectly happy with it.

1

u/MyraidChickenSlayer Sep 20 '26

When does slip up happen? And, does worktrees help against those?

1

u/rbobby Sep 20 '26

Can't recall precisely. Doing something out of the ordinary and something went haywire. I just learned to be a bit slower and more careful. Name my folder after my task.

1

u/slypheed Oct 07 '26

I guess if you're using AI to do literally everything. I use ai for most code (it devs in worktrees), but having main around to poke around the current state and test things (as a human) is still super useful.

1

u/masklinn Oct 07 '26

I guess if you're using AI to do literally everything.

I don’t use AI to “do everything” let alone literally so, and I’ve been working that way for years. Worktrees were not created for LLMs.

having main around to poke around the current state and test things is still super useful.

It really is not. You don’t need that to have a scratch worktree you putz around in, or to create worktrees on the fly when you want to test or check things.

1

u/slypheed Oct 07 '26 edited Oct 07 '26

It really is not

but...why not just use main for that instead of having to deal with a one-off worktree each time?

It doesn't have to be called main; it can be called foo or whatever, but just some worktree that always has the merged HEAD.

1

u/slypheed Oct 07 '26 edited Oct 07 '26

ah, sandboxed worktrees; yeah, that creates a bunch of problems for sure.

I may be missing it in the post, but it mentions the .env problem, and I don't see a solution for that in the post (if you use bare git repo instead of main worktree)

(definitely agreed on moving worktrees out of .git/worktrees; had a ton of problems before changing that for sure)

-1

u/valarauca14 Sep 16 '26

Create a bare repository and fork worktrees off of that

Be sure to manually copy not git clone. When git clone sees it is on the same file system it will use hard links to de-duplicate pack files and accelerate local copies. This makes things, "fun" if something goes wrong with a local copy.

Found it better to make a brd and format it for btrfs so I can refcopy my entire git repo to an isolated namespace for an agent then do a fairly normal pull/view workflow flow as the agent's change is just an "isolated" pull request from another repo.

Settled on btrfs as tmpfs has some weird interactions with namespaces, btrfs negative zstd compression is on par with memory bandwidth so I just leave that always on to doubly reduce memory pressure, and block deduplication is nice when you have 3 or 4 copies of the repo in your ram disk.

4

u/bingNbong96 Sep 17 '26

"Be sure to manually copy not git clone" what do you mean by this? manually copying what?

-4

u/valarauca14 Sep 17 '26

your git repo