Bird
Raised Fist0
Gitdevops~20 mins

Creating a worktree in Git - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Worktree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of creating a new worktree
What is the output of the following command if the branch feature-x does not exist yet?

git worktree add ../feature-x feature-x
Git
git worktree add ../feature-x feature-x
Aerror: pathspec 'feature-x' did not match any file(s) known to git
Bfatal: 'feature-x' is not a commit and a branch 'feature-x' cannot be created from it.
C
Preparing worktree (detached HEAD) in ../feature-x
Branch 'feature-x' set up to track remote branch 'feature-x' from 'origin'.
D
Preparing worktree in ../feature-x
Switched to a new branch 'feature-x'
Attempts:
2 left
💡 Hint
The command creates a new branch and a new worktree directory.
🧠 Conceptual
intermediate
1:30remaining
Purpose of git worktree
Which of the following best describes the purpose of using git worktree?
ATo create multiple working directories linked to the same repository, allowing simultaneous work on different branches.
BTo permanently delete branches from the repository history.
CTo clone a repository into a new directory with a different remote URL.
DTo merge two branches automatically without conflicts.
Attempts:
2 left
💡 Hint
Think about working on multiple features at the same time.
❓ Troubleshoot
advanced
2:00remaining
Error when adding a worktree
You run the command git worktree add ../test-branch origin/test-branch but get the error:

fatal: 'origin/test-branch' is not a commit and a branch 'origin/test-branch' cannot be created from it.

What is the most likely cause?
AThe directory '../test-branch' already exists and is not empty.
BThe remote branch 'test-branch' does not exist locally or remotely, so git cannot create a worktree from it.
CYou do not have permission to create directories in the parent folder.
DThe git repository is corrupted and needs to be reinitialized.
Attempts:
2 left
💡 Hint
Check if the remote branch exists before adding a worktree.
🔀 Workflow
advanced
2:30remaining
Steps to remove a worktree safely
Which sequence of commands correctly removes a git worktree located at ../feature-y and cleans up references?
A1,3,4
B2,3,4
C1,4,3
D3,1,4
Attempts:
2 left
💡 Hint
Use git commands to remove worktree and branch, then prune.
✅ Best Practice
expert
3:00remaining
Best practice for concurrent feature development
You want to work on two features simultaneously without switching branches in the same directory. Which is the best practice using git worktree?
AClone the repository twice into different folders and work on each feature separately.
BUse <code>git stash</code> to save changes and switch branches in the same directory repeatedly.
CCreate two separate worktrees, each checked out to a different feature branch, so you can work independently.
DCommit all changes to the main branch and then create feature branches later.
Attempts:
2 left
💡 Hint
Think about avoiding switching branches in one folder.

Practice

(1/5)
1. What is the main purpose of creating a Git worktree?
easy
A. To work on multiple branches simultaneously in separate folders
B. To merge two branches automatically
C. To delete a branch safely
D. To compress the repository size

Solution

  1. Step 1: Understand what a worktree is

    A Git worktree allows you to have multiple working directories linked to the same repository.
  2. Step 2: Identify the main use case

    This lets you work on different branches at the same time without switching branches in one folder.
  3. Final Answer:

    To work on multiple branches simultaneously in separate folders -> Option A
  4. Quick Check:

    Worktree = multiple branches, multiple folders [OK]
Hint: Worktrees = multiple branches open in separate folders [OK]
Common Mistakes:
  • Thinking worktree merges branches
  • Confusing worktree with branch deletion
  • Believing worktree compresses repo
2. Which of the following is the correct syntax to create a new worktree for branch feature in folder ../feature-folder?
easy
A. git worktree add ../feature-folder feature
B. git worktree create feature ../feature-folder
C. git add worktree feature ../feature-folder
D. git worktree new ../feature-folder feature

Solution

  1. Step 1: Recall the correct command structure

    The command to add a worktree is git worktree add <path> <branch>.
  2. Step 2: Match the syntax with options

    git worktree add ../feature-folder feature matches the correct syntax exactly.
  3. Final Answer:

    git worktree add ../feature-folder feature -> Option A
  4. Quick Check:

    git worktree add path branch = correct syntax [OK]
Hint: Remember: 'git worktree add path branch' is correct syntax [OK]
Common Mistakes:
  • Using 'create' instead of 'add'
  • Swapping branch and path order
  • Using 'git add worktree' which is invalid
3. Given the commands:
git worktree add ../test-branch test
cd ../test-branch
git branch --show-current

What will be the output of the last command?
medium
A. master
B. main
C. test
D. detached HEAD

Solution

  1. Step 1: Understand what 'git worktree add' does

    This command creates a new folder with the specified branch checked out.
  2. Step 2: Check current branch in new worktree

    After changing directory, git branch --show-current shows the branch name checked out, which is 'test'.
  3. Final Answer:

    test -> Option C
  4. Quick Check:

    Worktree folder shows branch name = test [OK]
Hint: Worktree folder shows the branch you added [OK]
Common Mistakes:
  • Assuming default branch 'main' or 'master'
  • Thinking it shows detached HEAD
  • Confusing branch names
4. You run git worktree add ../new-feature without specifying a branch. What will happen?
medium
A. Git creates a new worktree with the current branch checked out
B. Git creates a new branch automatically
C. Git throws an error asking for a branch name
D. Git creates a detached HEAD worktree at the current commit

Solution

  1. 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.
  2. Step 2: Understand the result

    This means the worktree is not on any branch but on a specific commit.
  3. Final Answer:

    Git creates a detached HEAD worktree at the current commit -> Option D
  4. Quick Check:

    No branch given = detached HEAD worktree [OK]
Hint: No branch? Worktree is detached HEAD at current commit [OK]
Common Mistakes:
  • Expecting an error without branch
  • Thinking it uses current branch name
  • Assuming a new branch is created
5. You want to work on two branches, main and feature, simultaneously. Your repo has no feature branch yet. Which sequence correctly creates a worktree for feature and switches to it?
hard
A. git worktree add ../feature feature cd ../feature
B. git branch feature git worktree add ../feature feature cd ../feature
C. git worktree add ../feature feature cd ../feature git checkout -b feature
D. git checkout -b feature git worktree add ../feature feature cd ../feature

Solution

  1. Step 1: Create the branch before adding worktree

    You must create the branch 'feature' first because it does not exist yet.
  2. Step 2: Add the worktree for the new branch and switch directory

    Then add the worktree pointing to 'feature' and change to that folder.
  3. Final Answer:

    git branch feature git worktree add ../feature feature cd ../feature -> Option B
  4. Quick Check:

    Create branch first, then add worktree [OK]
Hint: Create branch first, then add worktree for it [OK]
Common Mistakes:
  • Adding worktree before branch exists
  • Creating branch inside worktree folder
  • Forgetting to switch directory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard