Git worktree workflow with release branches

July 9, 2022  ‐ 2 min read

In most projects I use a branching model as described by Vincent Driessen with a dev, staging and prod branch.

For new feature development I branch off of dev, and once those changes are considered adequate dev gets promoted to staging. In most cases this works fine.

In some cases however a hotfix needs to be applied, for which I preferably branch off from staging. To make sure that not fully tested features don't land prematurely in production.

This can cause some hassle in my local git repo. With switching from dev to staging I need to make sure my node_modules are properly and there are no migrations applied which should not be applied on staging yet. Don't wanna mess around if I already need to apply a hotfix.

Enter git worktrees. Git worktrees allow you to checkout multiple worktrees in different directories while using the same repository. Thus, I have a separate directory for the staging branch which have their own node_modules assuming that you added that directory to the .gitignore.

Add a worktree

To create a new git worktree to your repository you can run the following command in your repository.

$ git worktree add <PATH> <BRANCH>

Lets for example say I have a repository in the folder ~/myproject. To add a worktree on the same level for the staging branch I run the following command.

~/myproject$ git worktree add ../myproject-staging staging