📖 Scenario: You are working on a software project using Git. Sometimes you need to work on multiple features or fixes at the same time without mixing changes. Git worktrees let you do this by creating separate folders for each task, all linked to the same repository.
🎯 Goal: Learn how to create and use Git worktrees to work on different branches in parallel without switching branches in the main folder.
📋 What You'll Learn
Create a new Git worktree linked to a specific branch
List existing Git worktrees
Switch to a worktree folder and check the current branch
Understand how worktrees allow parallel work on different branches
💡 Why This Matters
🌍 Real World
Developers often need to work on several features or bug fixes at once. Git worktrees let them keep separate folders for each task, avoiding confusion and mistakes.
💼 Career
Understanding Git worktrees helps you manage parallel development efficiently, a key skill for software developers and DevOps engineers.
Progress0 / 4 steps
1
Initialize a Git repository and create a branch
Run git init to create a new Git repository. Then make an initial commit using git commit --allow-empty -m "Initial commit". Then create a branch called feature1 using git branch feature1.
Git
Hint
Use git init to start a repo, git commit --allow-empty -m "Initial commit" to create the initial commit, and git branch feature1 to create the branch.
2
Create a new worktree for the feature1 branch
Use git worktree add ../feature1 feature1 to create a new worktree folder named feature1 linked to the feature1 branch.
Git
Hint
The command git worktree add creates a new folder linked to a branch.
3
List all existing worktrees
Run git worktree list to see all the worktrees connected to your repository.
Git
Hint
Use git worktree list to view all worktrees.
4
Check the current branch in the new worktree
Change directory to ../feature1 and run git branch --show-current to confirm you are on the feature1 branch.
Git
Hint
Use cd to enter the worktree folder and git branch --show-current to see the branch name.
Practice
(1/5)
1. What is the main benefit of using git worktree for parallel work?
easy
A. It deletes old branches to save space.
B. It automatically merges branches without conflicts.
C. It allows working on multiple branches simultaneously in separate folders.
D. It creates a backup of the entire repository.
Solution
Step 1: Understand what git worktree does
git worktree lets you check out multiple branches at once in different folders without cloning the repo multiple times.
Step 2: Compare options with this feature
Only It allows working on multiple branches simultaneously in separate folders. correctly describes this benefit. Other options describe unrelated or incorrect features.
Final Answer:
It allows working on multiple branches simultaneously in separate folders. -> Option C
Quick Check:
Parallel branch work = It allows working on multiple branches simultaneously in separate folders. [OK]
Hint: Worktrees = multiple branches in separate folders [OK]
Common Mistakes:
Thinking worktrees merge branches automatically
Confusing worktrees with backups
Assuming worktrees delete branches
2. Which command correctly creates a new worktree for branch feature in folder ../feature?
easy
A. git worktree create feature ../feature
B. git worktree add ../feature feature
C. git add worktree ../feature feature
D. git checkout worktree feature ../feature
Solution
Step 1: Recall correct syntax for adding a worktree
The correct syntax is git worktree add <path> <branch>.
Step 2: Match options to syntax
Only git worktree add ../feature feature matches this syntax exactly. Others use wrong commands or order.
Hint: Use 'git worktree add <path> <branch>' to create worktree [OK]
Common Mistakes:
Using 'create' instead of 'add'
Mixing order of arguments
Using 'git add' or 'git checkout' incorrectly
3. Given these commands executed in a repo:
git worktree add ../feature feature
cd ../feature
git status
What will git status show in the ../feature folder?
medium
A. On branch master, with changes from master branch.
B. Error: not a git repository.
C. Detached HEAD state with no branch.
D. On branch feature, with changes from that branch.
Solution
Step 1: Understand what git worktree add ../feature feature does
This creates a new folder ../feature checked out to the feature branch.
Step 2: Check git status in that folder
Since the folder is a valid worktree on feature branch, git status shows branch feature and its changes.
Final Answer:
On branch feature, with changes from that branch. -> Option D
Quick Check:
Worktree folder branch = feature [OK]
Hint: Worktree folder tracks specified branch, not master [OK]
Common Mistakes:
Assuming worktree defaults to master branch
Thinking worktree folder is detached HEAD
Confusing worktree folder with unrelated repo
4. You ran git worktree add ../dev feature but got an error: fatal: 'feature' is not a commit and a branch 'feature' cannot be created from it. What is the likely cause?
medium
A. The branch 'feature' does not exist in the repo.
B. The folder '../dev' already contains a git repo.
C. You forgot to commit changes before adding worktree.
D. You used 'git worktree create' instead of 'add'.
Solution
Step 1: Analyze the error message
The error says 'feature' is not a commit and branch cannot be created, meaning the branch does not exist.
Step 2: Match cause to options
The branch 'feature' does not exist in the repo. matches this cause. Other options describe unrelated issues or wrong commands.
Final Answer:
The branch 'feature' does not exist in the repo. -> Option A
Quick Check:
Branch must exist before adding worktree [OK]
Hint: Check branch exists before adding worktree [OK]
Common Mistakes:
Assuming folder conflict causes this error
Thinking uncommitted changes block worktree add
Confusing 'add' with 'create' command
5. You want to work on two features in parallel without cloning the repo twice. You create two worktrees: