0
0
Gitdevops~10 mins

Removing worktrees in Git - Step-by-Step Execution

Choose your learning style9 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.