git worktree: one working folder per branch
How to open several branches at once from a single repository, without stashing or cloning.
A worktree adds working folders while sharing the history. Three commands cover it: create, use, clean up.
Open another branch without interrupting your work
Sometimes an urgent fix lands in the middle of your work. To switch branches you have to commit or stash your changes first. You could clone the repository a second time, but then the history is duplicated, and what you fetch in one clone isn’t visible in the other.
With git worktree, one repository can have several working folders. The history and branches are shared, and each folder has a different branch open.
Create the new folders outside the main one. Inside it, they would show up as untracked files in the main folder.
As the figure below shows, there is one history and several working folders.
Create, use, clean up
Create a new working folder with git worktree add. With -b you also create a new branch at the same time.
# open a new branch called fix/typo in ../app-fix
git worktree add ../app-fix -b fix/typo
# list the folders that are open
git worktree listMove into the new folder and commit as you would in any repository. When you are done and have opened the PR, clean the folder up.
git worktree remove ../app-fixremove won’t delete a folder that has uncommitted changes. If any remain, either commit them or discard them first. The branch itself stays, so delete it separately if you no longer need it.
Things to know
- A branch can be open in only one working folder at a time. If you name a branch that is already open in another folder, you get an error.
- A working folder contains .git as a file. It holds a path pointing into the main repository’s .git. If you delete this file, Git no longer recognizes the folder.
- Install dependencies again in each folder. Untracked files such as node_modules are not shared.
- If you delete a folder by hand, tidy the records with git worktree prune. Leftover records can stop you from opening the same branch again.
When the folders pile up, check the current state with git worktree list. Clean up the ones you are finished with early.
When it fits
- You want to ship an urgent fix on another branch in the middle of your work
- You want to run someone else’s PR without moving your own branch
- You want to keep working while a long build or test runs
- You want an agent such as Claude Code to work on another branch (separate folders keep the work from mixing)