GitWorktree.org logoGitWorktree.org

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.

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:

Navigate with cd
# See which worktrees are available
git worktree list
# /home/user/project          abc1234 [main]
# /home/user/project-feature  def5678 [feature/auth]
# /home/user/project-hotfix   789abcd [hotfix/login]

# Switch to the feature worktree
cd /home/user/project-feature

# Work on feature/auth — all git commands operate on this worktree
git status
git add .
git commit -m "Add login validation"

# Switch back to main
cd /home/user/project

Each 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:

Shell function: switch by branch name
# 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 switcher with fzf
# 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 list

Terminal 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:

tmux sessions per worktree
# Create a tmux session per worktree
tmux new-session -d -s main -c /home/user/project
tmux new-session -d -s feature -c /home/user/project-feature
tmux new-session -d -s hotfix -c /home/user/project-hotfix

# Switch between them instantly
tmux switch-client -t main
tmux switch-client -t feature
tmux switch-client -t hotfix
Auto-create tmux sessions for all worktrees
# 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:

VS Code multi-root workspace
// project.code-workspace
{
  "folders": [
    { "path": "/home/user/project", "name": "main" },
    { "path": "/home/user/project-feature", "name": "feature/auth" },
    { "path": "/home/user/project-hotfix", "name": "hotfix/login" }
  ]
}

Alternatively, open each worktree in a separate VS Code window:

Separate VS Code windows
# Open each worktree in its own VS Code window
code /home/user/project
code /home/user/project-feature
code /home/user/project-hotfix

JetBrains 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 running git worktree list every 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 pushd and popd to maintain a directory stack if you jump between more than two worktrees frequently.