Course contentsShow
← Computer Science
Lesson 2212 of 2,87249. Version Control with GitFree lesson

Git Worktrees

Work on multiple branches simultaneously by creating separate working directories with git worktree.

Git Worktrees

You'll learn how to work on multiple branches at once without the constant hassle of switching branches and stashing changes.

What Are Git Worktrees?

Git worktrees let you create multiple working directories for the same repository, each checked out to a different branch. Instead of constantly switching branches in your main workspace (and potentially dealing with uncommitted changes), you can have separate directories for different tasks running simultaneously.

Think of it like having multiple desks in your office, each with a different project laid out. You can walk between them freely without clearing off one desk before working on another.

Why Use Worktrees?

When you're juggling multiple tasks—say, developing a feature on one branch while a critical bug needs fixing on another—traditional workflow requires:

  1. Stashing or committing incomplete work
  2. Switching branches
  3. Making fixes
  4. Switching back
  5. Restoring your previous state

With worktrees, you simply open a different directory window. Each worktree maintains its own working directory but shares the same Git repository data (commits, objects), saving disk space.

Creating a Worktree

# Add a new worktree in ../bugfix-dir for branch 'hotfix'
git worktree add ../bugfix-dir hotfix

# Create a new branch and worktree simultaneously
git worktree add ../new-feature -b feature/new-api

Now you can edit files in both your main directory and ../bugfix-dir simultaneously—they're independent workspaces.

Managing Worktrees

# List all worktrees
git worktree list

# Remove a worktree when done
git worktree remove ../bugfix-dir

Key Takeaway: Git worktrees enable parallel development by giving you multiple isolated working directories for the same repository, eliminating the need for constant branch switching when working on multiple tasks simultaneously.