Bird
Raised Fist0
Gitdevops~10 mins

Removing worktrees in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Removing worktrees
List worktrees
↓
Choose worktree to remove
↓
Run git worktree remove <path>
↓
Worktree directory deleted
↓
Worktree reference removed from git
↓
Confirm removal by listing worktrees
This flow shows how to safely remove a git worktree by listing, selecting, removing, and confirming its deletion.
Execution Sample
Git
git worktree list
# shows all worktrees

git worktree remove ./feature-branch
# removes the worktree at path

git worktree list
# confirms removal
This sequence lists existing worktrees, removes one by path, then lists again to confirm it is gone.
Process Table
StepCommandActionResultNotes
1git worktree listShow all worktreesLists main and feature-branch worktreesInitial state with 2 worktrees
2git worktree remove ./feature-branchRemove worktree at ./feature-branchDeletes directory and removes referenceWorktree removed successfully
3git worktree listShow all worktreesLists only main worktreefeature-branch no longer listed
4git worktree remove ./nonexistentAttempt to remove non-existing worktreeError: path is not a worktreeShows error if path invalid
5git worktree remove ./mainAttempt to remove main worktreeError: cannot remove main worktreeMain worktree cannot be removed
💡 Removal stops when worktree is deleted or error occurs if path invalid or main worktree targeted
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
worktrees[main, feature-branch][main][main][main][main]
command_resultN/ASuccessN/AError: path is not a worktreeError: cannot remove main worktree
Key Moments - 3 Insights
Why does 'git worktree remove' fail when trying to remove the main worktree?
The main worktree is the primary repository directory and cannot be removed with 'git worktree remove'. This is shown in step 5 of the execution_table where an error occurs.
What happens if you try to remove a path that is not a worktree?
Git returns an error saying the path is not a worktree, as shown in step 4 of the execution_table.
After removing a worktree, how can you confirm it was removed?
By running 'git worktree list' again, you see the removed worktree no longer appears, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What happens when 'git worktree remove ./feature-branch' runs?
AThe worktree directory is deleted and reference removed
BAn error occurs because the path is invalid
CNothing happens, command is ignored
DThe main worktree is removed
💡 Hint
Refer to the 'Result' column in step 2 of the execution_table
At which step does the command fail because the path is not a worktree?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Check the 'Action' and 'Result' columns in the execution_table for errors
If you remove the feature-branch worktree, what will the 'worktrees' variable contain after step 3?
A[feature-branch]
B[main]
C[main, feature-branch]
D[]
💡 Hint
Look at the variable_tracker row for 'worktrees' after step 3
Concept Snapshot
git worktree remove <path>
- Deletes the worktree directory at <path>
- Removes the worktree reference from git
- Cannot remove the main worktree
- Confirm removal with 'git worktree list'
- Errors if path is invalid or main worktree
Full Transcript
Removing a git worktree involves listing existing worktrees, choosing the one to remove, running 'git worktree remove <path>', and confirming removal by listing worktrees again. The main worktree cannot be removed this way. Errors occur if the path is not a worktree or if trying to remove the main worktree. This process safely deletes the worktree directory and cleans up git references.

Practice

(1/5)
1. What does the command git worktree remove <path> do?
easy
A. Deletes the specified extra worktree folder and unregisters it from Git.
B. Creates a new worktree at the specified path.
C. Lists all existing worktrees in the repository.
D. Resets the current branch to the specified commit.

Solution

  1. Step 1: Understand the command purpose

    The command git worktree remove <path> is used to remove an extra worktree folder from your Git repository.
  2. Step 2: Effect of the command

    It deletes the worktree folder at the given path and unregisters it from Git, keeping the project clean.
  3. Final Answer:

    Deletes the specified extra worktree folder and unregisters it from Git. -> Option A
  4. Quick Check:

    Remove worktree = Delete folder + unregister [OK]
Hint: Remove worktree means delete folder and unregister it [OK]
Common Mistakes:
  • Confusing remove with create or list commands
  • Thinking it resets branches
  • Assuming it only unregisters without deleting folder
2. Which of the following is the correct syntax to remove a worktree located at ./feature-branch?
easy
A. git worktree remove ./feature-branch
B. git worktree rm ./feature-branch
C. git remove worktree ./feature-branch
D. git worktree delete ./feature-branch

Solution

  1. Step 1: Recall correct command syntax

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

    git worktree remove ./feature-branch matches the correct syntax exactly: git worktree remove ./feature-branch.
  3. Final Answer:

    git worktree remove ./feature-branch -> Option A
  4. Quick Check:

    Remove worktree syntax = git worktree remove [OK]
Hint: Use 'git worktree remove' followed by path [OK]
Common Mistakes:
  • Using 'delete' instead of 'remove'
  • Swapping command order like 'git remove worktree'
  • Using shorthand 'rm' which is invalid here
3. Given you have a worktree at ./dev, what will be the output of running git worktree list after git worktree remove ./dev?
medium
A. The list will still show ./dev but marked as removed.
B. The list will no longer show the ./dev worktree.
C. The command will fail with an error about ./dev.
D. The list will show ./dev and all other worktrees unchanged.

Solution

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

    It deletes the worktree folder and unregisters it from Git, so it is no longer tracked.
  2. Step 2: Effect on 'git worktree list'

    After removal, the worktree at ./dev will not appear in the list anymore.
  3. Final Answer:

    The list will no longer show the ./dev worktree. -> Option B
  4. Quick Check:

    Remove worktree means it disappears from list [OK]
Hint: Removed worktrees vanish from 'git worktree list' output [OK]
Common Mistakes:
  • Expecting removed worktree to still appear in list
  • Thinking it shows as 'removed' but still listed
  • Assuming command errors out after removal
4. You tried to remove a worktree with git worktree remove ./test but got an error saying the path is not a worktree. What is the likely cause?
medium
A. The repository is corrupted and needs reinitialization.
B. You need to use git worktree delete instead of remove.
C. You must be on the main branch to remove any worktree.
D. The path ./test does not exist or is not registered as a worktree.

Solution

  1. Step 1: Analyze the error message

    The error says the path is not a worktree, meaning Git does not recognize it as one.
  2. Step 2: Identify common causes

    This usually happens if the path does not exist or was never added as a worktree.
  3. Final Answer:

    The path ./test does not exist or is not registered as a worktree. -> Option D
  4. Quick Check:

    Path must exist and be registered to remove [OK]
Hint: Check path exists and is registered worktree before removing [OK]
Common Mistakes:
  • Using wrong command like 'delete' instead of 'remove'
  • Assuming branch affects removal
  • Thinking repo corruption causes this error
5. You have multiple worktrees: ./feature1, ./feature2, and ./feature3. You want to remove ./feature2 but keep its changes saved. What is the best approach?
hard
A. Run git worktree remove ./feature2 without saving changes; Git saves them automatically.
B. Directly delete the ./feature2 folder without Git commands.
C. Commit or stash changes in ./feature2, then run git worktree remove ./feature2.
D. Switch to main branch and delete ./feature2 folder manually.

Solution

  1. Step 1: Preserve changes before removal

    To keep changes in a worktree, you must commit or stash them before removing the worktree.
  2. Step 2: Remove the worktree properly

    After saving changes, use git worktree remove ./feature2 to safely remove the worktree folder and unregister it.
  3. Final Answer:

    Commit or stash changes in ./feature2, then run git worktree remove ./feature2. -> Option C
  4. Quick Check:

    Save changes first, then remove worktree [OK]
Hint: Save changes (commit/stash) before removing worktree [OK]
Common Mistakes:
  • Deleting folder manually causing data loss
  • Assuming Git auto-saves changes on remove
  • Removing without saving changes first
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