What if you could work on two features at once without losing your place or risking mistakes?
Creating a worktree in Git - Why You Should Know This
Imagine you are working on two different features in the same project. You need to switch back and forth between branches frequently. You try to do this by changing branches in your single project folder.
Switching branches manually means you lose your current changes or have to stash them every time. It's slow and risky because you might forget to stash or accidentally overwrite work. Managing multiple features becomes confusing and error-prone.
Creating a worktree lets you have multiple working directories linked to the same repository. Each worktree can be on a different branch, so you can work on multiple features side-by-side without switching or losing changes.
git checkout feature1
// work
git checkout feature2
// stash changes or risk conflictsgit worktree add ../feature1 feature1 cd ../feature1 // work on feature1 cd - git worktree add ../feature2 feature2 cd ../feature2 // work on feature2 simultaneously
You can work on multiple branches at the same time, speeding up development and reducing mistakes.
A developer can fix a bug on the main branch while simultaneously developing a new feature on another branch, without losing progress or switching back and forth.
Manual branch switching is slow and risky.
Worktrees create separate folders for different branches.
This allows parallel work without conflicts or lost changes.
Practice
Solution
Step 1: Understand what a worktree is
A Git worktree allows you to have multiple working directories linked to the same repository.Step 2: Identify the main use case
This lets you work on different branches at the same time without switching branches in one folder.Final Answer:
To work on multiple branches simultaneously in separate folders -> Option AQuick Check:
Worktree = multiple branches, multiple folders [OK]
- Thinking worktree merges branches
- Confusing worktree with branch deletion
- Believing worktree compresses repo
feature in folder ../feature-folder?Solution
Step 1: Recall the correct command structure
The command to add a worktree isgit worktree add <path> <branch>.Step 2: Match the syntax with options
git worktree add ../feature-folder feature matches the correct syntax exactly.Final Answer:
git worktree add ../feature-folder feature -> Option AQuick Check:
git worktree add path branch = correct syntax [OK]
- Using 'create' instead of 'add'
- Swapping branch and path order
- Using 'git add worktree' which is invalid
git worktree add ../test-branch test cd ../test-branch git branch --show-current
What will be the output of the last command?
Solution
Step 1: Understand what 'git worktree add' does
This command creates a new folder with the specified branch checked out.Step 2: Check current branch in new worktree
After changing directory,git branch --show-currentshows the branch name checked out, which is 'test'.Final Answer:
test -> Option CQuick Check:
Worktree folder shows branch name = test [OK]
- Assuming default branch 'main' or 'master'
- Thinking it shows detached HEAD
- Confusing branch names
git worktree add ../new-feature without specifying a branch. What will happen?Solution
Step 1: Check behavior when branch is omitted
If no branch is specified, Git creates a worktree at the current commit in detached HEAD state.Step 2: Understand the result
This means the worktree is not on any branch but on a specific commit.Final Answer:
Git creates a detached HEAD worktree at the current commit -> Option DQuick Check:
No branch given = detached HEAD worktree [OK]
- Expecting an error without branch
- Thinking it uses current branch name
- Assuming a new branch is created
main and feature, simultaneously. Your repo has no feature branch yet. Which sequence correctly creates a worktree for feature and switches to it?Solution
Step 1: Create the branch before adding worktree
You must create the branch 'feature' first because it does not exist yet.Step 2: Add the worktree for the new branch and switch directory
Then add the worktree pointing to 'feature' and change to that folder.Final Answer:
git branch feature git worktree add ../feature feature cd ../feature -> Option BQuick Check:
Create branch first, then add worktree [OK]
- Adding worktree before branch exists
- Creating branch inside worktree folder
- Forgetting to switch directory
Ready to prove it?
Pass the test and this pattern moves unknown
