Git worktree allows us to maintain multiple working directories for a single repository, the benefits of this are that we can work on multiple branches at the same time.
Ofcourse your first question might be, well I can checkout a repo multiple times in different folders/directories already, so what’s the point?
Let’s compare multiple checkouts against git worktree
- Disk usage
- Multiple checkouts: Each clone has a full .git directory, incl. all refs etc.
- Git worktree: All work trees share a single .git folder and hence lightweight
- Performance
- Multiple checkouts: Cloning is slower, esp on large repos
- Git worktree: As no clone takes place and reuses .git this is very fast
- Consistency
- Multiple checkouts: Clones can drift, i.e. different removes, configs etc.
- Git worktree: As worktree’s share .git they share remotes, configs etc.
- Branch Isolation
- Multiple checkouts: With multiple checkouts, can become confusing
- Git worktree: Only allows one instance of a branch to be checked out, avoid divergence
- Maintenance Overhead
- Multiple checkouts: Multiple clones means multiple .git and folders to maintain
- Git worktree: A central .git folder with lightweight trees
- Use Cases
- Multiple checkouts: Great if using multiple remotes and sandboxing
- Git worktree: Great for parallel branch work
Usage
I find it easiest if you have a folder, for example if your application was called MyApp then the folder is MyApp, but then clone your repo into MyApp and name it main (or whatever your root branch is named). The reason I find this best is because when we start using git worktree I want the branches etc. to be next to the main branch, as opposed to mingling with other cloned applications – just makes it simpler to see pretty quickly what worktree’s you have.
If we there assume I have something like c:\repos\myapp\main now cd in the main folder and run
git worktree list
This will show any worktree’s you have and their file location.
Let’s create a new worktree
git worktree add ../ka-hotfix -b hotfix
If you already have a branch named hotfix then you can ignore the -b.
This will have created a new worktree named hotfix in the folder ../ka-hotfix – at this point you’re see if you run git worktree list again that the branch was created and the worktree path is a sibling of our main folder.
If you cd into your branches folder you’ll notice that if you had changes in main, the branch is clean (i.e. no changes). You therefore do not need to stash changes if you’re switching between branches in a single folder (i.e. not using worktree’s).
From either main or you new branch worktree you’ll find git branch list all the branches because it’s using the same .git folder etc.
When we have a branch checked out we cannot check it out again, i.e. the root .git shows it’s already checked out into a different worktree.
We can delete a worktree, but if we run git worktree list again it still shows in the list even though it’s been deleted – it should be displayed as prunable. We can now run
git worktree prune
to remove anything no longer in a worktree.
The thing to remember is that the branch still exists, it’s just the worktree removed.
I’m not going to go into every worktree command but the following is a list of some of the options/subcommands for you the investigate
git worktree add git worktree list git worktree move git worktree remove git worktree repair git worktree lock git worktree unlock