0
0
Gitdevops~5 mins

Removing worktrees in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you create extra working copies of your git repository called worktrees to work on different branches. When you finish, you need to remove these worktrees to keep your workspace clean and avoid confusion.
When you have finished working on a feature branch in a separate worktree and want to delete it.
When you want to free up disk space by removing unused worktrees.
When you accidentally created a worktree and want to clean it up.
When you want to tidy your git repository by removing old or stale worktrees.
Commands
This command shows all the current worktrees linked to your repository so you can see which ones exist before removing any.
Terminal
git worktree list
Expected OutputExpected
/home/user/my-repo 123abc [main] /home/user/my-repo-feature 456def [feature-branch]
This command removes the worktree located at /home/user/my-repo-feature, cleaning up the extra working copy safely.
Terminal
git worktree remove /home/user/my-repo-feature
Expected OutputExpected
Removing worktree /home/user/my-repo-feature
Run this again to verify that the worktree was successfully removed and no longer appears in the list.
Terminal
git worktree list
Expected OutputExpected
/home/user/my-repo 123abc [main]
Key Concept

If you remember nothing else from this pattern, remember: always use 'git worktree remove' with the full path to safely delete a worktree.

Common Mistakes
Deleting the worktree folder manually without using git commands.
This leaves git metadata in an inconsistent state, causing errors or confusion about existing worktrees.
Always use 'git worktree remove <path>' to properly unregister and delete the worktree.
Trying to remove the main repository folder as a worktree.
The main repository is not a worktree and cannot be removed with this command; it will cause errors.
Only remove additional worktrees created with 'git worktree add', not the main repository.
Summary
Use 'git worktree list' to see all current worktrees.
Use 'git worktree remove <path>' to safely delete a specific worktree.
Verify removal by listing worktrees again to confirm cleanup.