Git worktrees: a hotfix without leaving your feature branch
A Git repository can have more than one working folder. git worktree add checks out another branch into a second folder that shares the same repository: the same history, the same remotes, no second clone. You can fix a bug on main in one folder while the half-finished redesign stays untouched, with its uncommitted changes and its running build, in the other.
Problem: In the middle of a two-week theme redesign, a production bug needs a fix now, and stashing the work, switching branches and rebuilding the assets back and forth is slow and risky.
Solution: Create a second worktree for the hotfix branch next to the project with git worktree add ../theme-hotfix -b hotfix/menu main, fix, commit and push there, then remove it with git worktree remove. The feature folder is never touched.
The hotfix
cd ~/projects/hello-theme # on feature/redesign, with uncommitted changes git fetch git worktree add ../hello-theme-hotfix -b hotfix/mobile-menu origin/main cd ../hello-theme-hotfix # fix, test git commit -am "Fix the mobile menu toggle on iOS" git push -u origin hotfix/mobile-menu cd ../hello-theme git worktree remove ../hello-theme-hotfix
The new folder starts from origin/main on a new branch. Dependencies are not shared: run npm ci or composer install in the new folder if the fix needs a build. A branch can be checked out in only one worktree at a time, which prevents two folders from fighting over the same branch.
Testing in WordPress
A theme or plugin worktree is just a folder, so a local WordPress can load it next to the main one with a symlink:
ln -s ~/projects/hello-theme-hotfix ~/Sites/example/wp-content/themes/hello-theme-hotfix wp theme activate hello-theme-hotfix --path=~/Sites/example
Switch the theme back after testing. A second local site, or a disposable environment such as wp-env, avoids changing your everyday setup.
Long-lived worktrees
git worktree add ../hello-theme-review # for reviewing pull requests git -C ../hello-theme-review switch feature/new-footer git worktree list git worktree prune # clean up entries of deleted folders
A permanent review folder lets you check out a colleague’s branch without stopping your own work.
NOTE: For a two-minute interruption, git stash is simpler (see git stash in a WordPress workflow); worktrees pay off when the interruption needs its own build, its own running server or more than one sitting. Delete a worktree with git worktree remove, not with rm -rf, or Git keeps a stale entry until git worktree prune.