Worktrees are one of my favourite git features, and something I think is widely underused.
An absolute dream for multitaskers (or those like myself, who try & fail to do so) — worktrees can be used to quickly & easily have multiple branches of your repository on your machine at one time, side-by-side.
Find yourself working on a feature, but a colleague needs help with an issue on their branch? No longer do you need to stash your changes, checkout their branch, stop, re-build, and re-run the project etc.
Simply navigate to the root of your project (containing your .git directory) and run:
git worktree add <-path-> <-branch->
So for this example:
git worktree add ../worktree-1 colleague-branch-name
The add command in this scenario takes two parameters:
The first is the path where you wish to create the new worktree — note I’ve gone up one directory, so as to not pollute my current branch. This will create a new directory named worktree-1.
The second is the branch name you wish to checkout - this will be the checked-out branch for the worktree, in this case colleague-branch-name.
After running this command, navigate up a directory and you will see something like:
original-project
|--> .git (Main)
worktree-1
|--> .git (colleague-branch-name)
Entering the worktree-1 directory and running a git status will confirm that we are now on the branch colleague-branch-name.
You are now free to make changes, test fixes, and create/push commits to this remote branch; all without interfering with your existing work.
It’s good advice not to clutter your source directory with worktrees. Personally, I keep all my worktrees in their own directory e.g. ~/wt.
Once you’ve pushed the fix for your colleague’s problem (like a helpful coworker), you can remove the worktree.
This is done by returning to our original-project directory containing our root .git directory, and running:
git worktree remove <-path->
So for this example:
git worktree remove ../worktree-1
The remove command can be executed with one parameter — the path to our worktree that we want to remove.
If you need to create a new branch, to work on a bug fix for example, you can pass the familiar -b flag to create this at the same time:
git worktree add <-path-> -b <-branch->
E.g.
git worktree add ~/wt/bugfix_XXXXX -b bugfix_XXXXXX
That’s it! That simple—but very effective in the right circumstances.