Git worktrees allow you to check out multiple branches of the same repository into separate directories simultaneously, eliminating the need to stash or commit work-in-progress every time you switch context between a feature and a hotfix. On a Linux WordPress development server, worktrees are particularly useful when you need to run two versions of a site in parallel — one serving a feature branch and another serving the stable main branch — without duplicating the entire repository. Each worktree has its own working directory and index but shares the full Git object database and history of the main clone, so switching between branches costs only the disk space of the checked-out files. Creating a worktree with git worktree add accepts the target directory path and an existing or new branch name, making the new working tree ready in one command. The --track flag links the new branch to a remote-tracking branch, so git pull inside the worktree stays current with the remote automatically. A linked worktree is listed with git worktree list and removed cleanly with git worktree remove, which also removes the directory if it is empty. Configuring two separate Nginx virtual hosts — one pointing to the main worktree path and another to the feature worktree path — serves each branch at a distinct URL without any symlink juggling. Each virtual host can reference the same WordPress database or a separate one by adjusting the wp-config.php path via an include from a non-tracked environment file outside the repository root. The Git hooks deployment guide explains how to trigger cache flushes automatically when a worktree branch receives a push. The branching strategies post covers the team workflow that makes parallel worktrees most valuable — a stable main branch and short-lived feature branches ready to test side by side. Worktrees cannot share a branch — attempting to check out the same branch in two worktrees produces an error, which prevents accidental conflicting edits to a single branch from two directories.
Problem: Switching between a WordPress feature branch and a production hotfix requires stashing or committing in-progress work, disrupting the development flow and risking uncommitted changes being left behind.
Solution: Use git worktree add to check out each branch into a dedicated directory, then point separate Nginx virtual hosts at each worktree path to serve and test both branches simultaneously.
# Add a worktree for a feature branch (linked to remote)
git worktree add --track -b feature/new-checkout ../site-feature origin/feature/new-checkout
# Add a worktree for a hotfix branch from the current HEAD
git worktree add ../site-hotfix -b hotfix/login-fix
# List all worktrees
git worktree list
# Remove a worktree when done
git worktree remove ../site-hotfix
# /etc/nginx/sites-available/site-main.conf
server {
listen 80;
server_name main.local;
root /var/www/site-main;
include snippets/wordpress.conf;
}
# /etc/nginx/sites-available/site-feature.conf
server {
listen 80;
server_name feature.local;
root /var/www/site-feature;
include snippets/wordpress.conf;
}
NOTE: A worktree directory must not already exist when running git worktree add — if the path exists, Git aborts with an error. Use a directory outside the main repository root so the worktree path is not accidentally tracked or excluded by the project .gitignore.