2025-12-05, 1 minutes reading for Software Engineers
Manage multiple working trees attached to the same repository.
A git repository can support multiple working trees, allowing you to check out more than one branch at a time
While working on a feature branch, your mate taps your shoulder about an issue in Prod; you sigh, then proceed to:
1. git stash -u
2. git switch -c hotfix prod_branch
3. git add ...; git commit ... ; git push...
# sometime has passed since then
4. git switch my_previous_branch
5. git stash list --stat
# Second guess yourself whether you actually did some work
# or these stashes belong to some other branch or hotfix
# Swear to yourself that next time you will pass `-m` when stashing
6. git stash pop
# Stash pop fails because force-of-habit made you accidentally pull
# from latest right after switching
So instead of the above; you can simply:
1. git worktree add -b hotfix ../.worktree/hotfix prod_branch
2. cd ../.worktree/hotfix
3. git add ...; git commit ... ; git push...
1. git worktree add -b hotfix ../.$(basename $PWD)-worktree/hotfix prod_branch
2. cd ../.$(basename $PWD)-worktree/hotfix
3. git add ...; git commit ... ; git push...
I mostly use to use it for pushing a hotfix, however nowadays with LLMs, I find myself working on 2 features branches at a time.
➜ blog git:(master) ✗ git worktree list
/home/.../blog b5cca2b [master]
/home/.../.blog-worktree/feature1 a84a6b7 [feature1]
/home/.../.blog-worktree/feature2 b13e4b2 [feature2]
/home/.../.blog-worktree/hotfix f95e1c5 [hotfix]
Now you might reasonably ask, how is that different from git cloning the same repo?
.git object database, so you are not cloning the whole git history, you save storage space.Even VS Code is aware of git worktree: