How to Switch Between Git Worktrees
Unlike branches, where you run git switch or git checkout to change context, git worktree directories are independent working copies. Switching between them is simply a matter of changing directories. There is no Git command to "switch worktrees" — you navigate to a different folder on your filesystem.
Try It: Switching Between Worktrees
cd, cd -, and instant context switching — no stashing needed
You've got three worktrees open for the acme project — your payment feature, the hotfix, and a PR review. Unlike git switch or git checkout, worktrees don't need a Git command to switch between them. You just cd into a different directory. Let's learn fast navigation tricks.
acme/ has the real .git/ directory. acme-hotfix/, acme-review/ each have a tiny .git file that points back — no duplicate history, no wasted disk.
Switching by Changing Directories
The most basic way to switch between worktrees is to use cd. Each worktree is a regular directory with the full project files checked out:
# See which worktrees are available
git worktree list
# /home/you/code/acme d1e2f3a [feature/payments]
# /home/you/code/acme-hotfix 7a8b9c0 [hotfix/checkout-crash]
# /home/you/code/acme-review b2c3d4e [feature/auth-refactor]
# Switch to the hotfix worktree
cd /home/you/code/acme-hotfix
# Work on hotfix/checkout-crash — all git commands operate here
git status
git add src/checkout.ts
git commit -m "fix: null check on cart total"
# Switch back to your feature
cd /home/you/code/acmeEach worktree has its own working directory, index (staging area), and HEAD. When you cd into a worktree, all Git commands automatically operate on that worktree's branch.
Navigating with Shell Aliases
Typing full paths gets tedious. Shell functions and aliases make switching faster:
# Add to your .bashrc or .zshrc
# Jump to a worktree by branch name
wt() {
local dir
dir=$(git worktree list --porcelain \
| awk -v branch="refs/heads/$1" \
'/^worktree /{dir=$2} /^branch /{if($2==branch) print dir}')
if [ -n "$dir" ]; then
cd "$dir" || return
echo "Switched to worktree: $dir (branch: $1)"
else
echo "No worktree found for branch '$1'"
return 1
fi
}
# Usage:
# wt main
# wt feature/auth
# wt hotfix/login# Interactive worktree switcher using fzf
wti() {
local selected
selected=$(git worktree list | fzf --height 40% --reverse)
if [ -n "$selected" ]; then
cd "$(echo "$selected" | awk '{print $1}')" || return
fi
}
# Usage: just type 'wti' and pick from the listTerminal Multiplexer Workflow
Tools like tmux and screen pair exceptionally well with worktrees. Dedicate one terminal session (or window/pane) to each worktree for instant switching:
# Create a tmux session per worktree
tmux new-session -d -s payments -c /home/you/code/acme
tmux new-session -d -s hotfix -c /home/you/code/acme-hotfix
tmux new-session -d -s review -c /home/you/code/acme-review
# Switch between them instantly
tmux switch-client -t payments
tmux switch-client -t hotfix
tmux switch-client -t review# Automate: create a tmux session for each active worktree
wt-sessions() {
git worktree list --porcelain | while IFS= read -r line; do
case "$line" in
worktree\ *)
dir="${line#worktree }"
name=$(basename "$dir")
tmux new-session -d -s "$name" -c "$dir" 2>/dev/null \
&& echo "Created session: $name"
;;
esac
done
}With this setup, switching between worktrees is as fast as pressing your tmux prefix key followed by the session name — no need to remember directory paths.
IDE-Based Switching
Most modern editors support opening multiple project directories. In VS Code, you can use workspaces to manage worktrees:
// acme.code-workspace
{
"folders": [
{ "path": "/home/you/code/acme", "name": "feature/payments" },
{ "path": "/home/you/code/acme-hotfix", "name": "hotfix/checkout-crash" },
{ "path": "/home/you/code/acme-review", "name": "feature/auth-refactor" }
]
}Alternatively, open each worktree in a separate VS Code window:
# Open each worktree in its own VS Code window
code /home/you/code/acme
code /home/you/code/acme-hotfix
code /home/you/code/acme-reviewJetBrains IDEs (IntelliJ, WebStorm, etc.) work similarly — open each worktree directory as a separate project. The IDE automatically detects the correct Git branch for each window.
Tips for Fast Switching
- Use a consistent naming convention for worktree directories (e.g.,
project-<branch-name>) so you can predict paths without runninggit worktree listevery time. - Group worktrees in one parent directory (e.g.,
~/worktrees/project/) for easy tab completion and browsing. - Set your terminal prompt to display the current Git branch. This makes it immediately obvious which worktree you are in after switching.
- Use
cd -to toggle between the two most recently visited directories. This is the fastest way to bounce between two worktrees. - Use
pushdandpopdto maintain a directory stack if you jump between more than two worktrees frequently.