Whenever you are developing something, context switching is something we all come across. Lucky for us there are multiple ways one can achieve this, even with in Git. Here we will be looking at one of the most useful tool for this, Git Worktree.
Table of Contents:
When to use Git Worktrees?
After you spend some time working with git on a project, there comes a situation where you have to switch between multiple branches either for applying some quick fix, work on something of higher priority, or checking a PR/code review, etc. Normally one would use a feature like git-stash
or create clones of repos in multiple directories and work othem. But these solutions often have an overhead of time and complexity.
Now, what can we do about this? The solution you are looking for may very well be Git Worktrees. This is a feature provided by git that allows you to have multiple branches checked out at the same time by creating multiple clone of a single repository.
How does Git Worktree work?
You can switch to a different branch and do the necessary work without affecting your current work directory. The basic workflow will have a structure like this:
- Create a replica of the project and witch to a new branch
- Work on the task
- Commit and push
- Back to the prior working directory and continue your work
Create Worktree
Let’s take a look at an example. Suppose you are working on a project. Just for the fun of it, letβs name it Patrick. Now you want to add a feature appendage
. To incorporate worktree, you first need to use the command git worktree add
. This command creates a worktree along with a branch that is named after the final word in your path. Then you can navigate to the path that you provided and start working on the task.
git worktree add <PATH> # Create feature-x directory and branch with the same name. git worktree add ../appendage

When you want to give your branch a name then you can use the -b
flag
git worktree add -b appendage ../appendage ββ
Switching Worktree
You are now happily working on the appendage
feature. Oh no, an issue appears in the application (letβs say sea-turtle
). To work on this you can go to the main repo directory and run the command git worktree add ../sea-turtle
. This almost instantly creates a new working copy of the repository at <main_directory>/sea-turtle
on a new branch called sea-turtle
. You can now go to the directory, discover the issue, apply necessary fixes, commit and push for review, and then return to the feature you were previously working on <main_directory>/appendage
. No hassle for stashing and no need to worry about pre-commit hooks not running.
You can work on many things in parallel, pausing one, starting another, returning back to it and more back and forth.
You can view the list of work trees using list command git worktree list

Removing Worktree
And to remove any worktree you can use the remove
command:
git worktree remove <name-of-worktree> git worktree remove appendage

Conclusion
Git Worktree is a nifty feature that allows you to switch context easily without altering the main working directory.
But you should be careful as some tools may not fully support it. Worktrees rely on the fact that git allows .git to be a file that points to a directory, instead of an actual directory. Some tools incorrectly assume that .git must be a directory and run into issues with worktrees. Regardless, this is an awesome and convenient tool to have in your tool box.