Git worktree: several things at once without the chaos

How to work on several branches at once without switching and stashing changes. The basic git worktree commands, pitfalls and use with AI assistants.

Tomáš Renčík5 min read
Illustration for the article: Git worktree: several things at once without the chaos
Contents
  1. 1. What a worktree is
  2. 2. Basic commands
  3. 3. How I use it
  4. 4. What to watch out for
  5. 5. Practice exercise
  6. 6. Frequently asked questions
  7. 7. When you don't need a worktree
  8. 8. Worktree and working with AI assistants
  9. 9. Final thoughts
  10. 10. Related articles

In short: Git worktree lets you have one repository checked out into several folders at once, each on a different branch. That way you can fix a bug right next to a half-finished feature without git stash and branch switching — and when working with AI assistants, each one can work in its own folder.

You know the feeling: you're working on a new feature, you have files half-edited, and suddenly someone says “login is broken, it needs fixing right now”. What now?

Most people do one of these:

  • git stash, switch branch, fix it, switch back, git stash pop — and hope nothing gets mixed up,
  • or commit the half-finished code with the message “wip”.

There's a better way: git worktree.

What a worktree is

Normally you have one repository and one working folder. Switching branches changes the files in that folder.

A worktree lets you have several working folders from one repository, each on a different branch. They share the history and the objects, so it doesn't take up space like a fresh clone. But each has its own files.

Basic commands

A new folder with a new branch:

git worktree add ../projekt-oprava -b oprava-prihlasenia main

This creates the folder ../projekt-oprava with the branch oprava-prihlasenia created from main. Your original folder with the work in progress stays untouched.

A list of all working folders:

git worktree list

When you're done, you remove the folder:

git worktree remove ../projekt-oprava

And if you deleted the folder by hand, git will clean up after it:

git worktree prune

How I use it

  • The main folder stays on the main branch. That's where I release and deploy from.
  • Every larger task gets its own worktree and its own branch.
  • Every worktree has its own development server on a different port. So I can have three versions of the application open at once and compare them.

It pays off most when working with AI. When three AI assistants are working on three tasks at once, each is in its own folder and they don't overwrite each other's files. At the end I merge their branches and resolve the conflicts in one go.

What to watch out for

1. A branch can only be in one worktree. Git won't let you have the same branch checked out twice — and that's a good thing.

2. Dependencies and local data aren't shared. node_modules, the local database, .env files — each folder has its own. A new worktree needs its dependencies installed, and you need to copy the local database over if you need it.

3. Don't switch branches under a running server. This always applies, not just with worktrees. When the development server is running and you switch branches, files disappear from under it and it starts crashing with nonsensical errors. Worktree solves this problem — instead of switching, you open a new folder.

4. Tidy up. Old worktrees take up disk space (mainly node_modules). Every now and then run git worktree list and remove the ones you don't need. And if servers are running in them, stop those first.

Practice exercise

Try it on any project of your own:

  1. In the main folder, start a change in two files and don't commit it.
  2. Create a worktree for an “urgent fix”:
    git worktree add ../projekt-oprava -b oprava main
    
  3. In the new folder, make a small change, commit it and merge it into main.
  4. Go back to the original folder — your work in progress is untouched.
  5. Tidy up: git worktree remove ../projekt-oprava and git branch -d oprava.

The whole thing takes ten minutes, and from then on you won't need git stash as often.

Frequently asked questions

Will it take up a lot of space?

The history isn't duplicated, only the working folder's files are. Dependencies such as node_modules usually take up the most space.

Can I move a worktree?

Yes, with git worktree move. Don't move it by hand in the file system — git would lose track of it.

Does it work with my editor?

Every worktree is an ordinary folder. You open it in your editor as a separate project, in two windows at once if you like.

What if I forget which worktree is where?

git worktree list shows you all of them, along with the branch and the latest commit.

When you don't need a worktree

For small things that take five minutes, an ordinary branch is enough. A worktree pays off when:

  • you're working on several things in parallel,
  • you need to compare two versions of the application side by side,
  • you don't want to risk your work in progress getting mixed up with a fix.

Worktree and working with AI assistants

Where worktree really shows its strength is working with several AI assistants at once. I give each one its own folder, its own branch and its own port for the development server. One works on the mobile interface, the second on search, the third on a fix in invoices. They don't overwrite each other's files, and I review each branch separately.

When they're done, I merge them into one shared branch. If two changes touched the same file, I resolve the conflict once, in one place — not continuously in the chaos of a single folder. Without worktree, this way of working wouldn't be possible.

Final thoughts

Worktree is one of those tools that after a week makes you say “how did I ever live without it”. Try it on your next fix — and you'll no longer be afraid of breaking your work in progress.

  • #git
  • #workflow
  • #vetvy
  • #produktivita