Common Git Worktree Mistakes and How to Fix Them
I tried to remove a git worktree and got an error. The command failed because my working directory wasn’t clean. This is one of the most common git worktree problems developers face.
Git worktrees are powerful for parallel development, but they come with their own set of pitfalls. Here are the five most common mistakes and how to fix each one.
Mistake 1: Removing a Worktree with Uncommitted Changes
I ran this command to remove a worktree:
git worktree remove feature-branchAnd got this error:
fatal: cannot remove a locked worktreeOr sometimes:
fatal: 'feature-branch' contains modified or untracked filesGit protects you from losing work. The worktree has changes that aren’t committed.
How to Fix It
Option 1: Commit or stash your changes first
git add .git commit -m "Save work in progress"git worktree remove feature-branchOption 2: Stash the changes
git stashgit worktree remove feature-branchOption 3: Force the removal (lose all changes)
git worktree remove -f feature-branchUse -f only when you’re sure you don’t need the uncommitted work.
Mistake 2: Manually Deleting a Worktree Directory
I deleted a worktree directory manually:
rm -rf ../my-featureThen I ran git worktree list and saw:
/Users/dev/main-project abc1234 [main]/Users/dev/my-feature def5678 [feature] <- This directory doesn't exist anymore!The git worktree list still shows the old path. Git doesn’t know the directory is gone.
How to Fix It
Use the prune command to clean up stale references:
git worktree pruneNow check the list:
git worktree list/Users/dev/main-project abc1234 [main]The stale reference is gone. You can also see what would be pruned first:
git worktree prune --dry-runMistake 3: Moving the Main Repository Breaks Worktree Paths
I moved my main repository to a different location:
mv ~/projects/my-app ~/workspace/my-appThen my worktrees stopped working:
fatal: /Users/dev/workspace/my-app is not a git repositoryWorktrees store absolute paths. Moving the main repository breaks these references.
How to Fix It
Git 2.17+ includes a repair command:
git worktree repairThis command updates all the path references automatically.
If you’re on an older Git version, you’ll need to remove and recreate the worktrees:
git worktree remove broken-worktreegit worktree add ../new-worktree branch-nameCheck your Git version:
git --versionMistake 4: Accidentally Operating on the Wrong Worktree
I wanted to run a destructive operation on my main worktree. But I was in a feature worktree directory. I almost deleted files I didn’t mean to.
Git worktrees don’t have protection against accidental operations by default.
How to Fix It
Lock a worktree to prevent accidental removal:
git worktree lock feature-worktreeNow if someone tries to remove it:
git worktree remove feature-worktreefatal: cannot remove a locked worktreeUnlock when you’re ready to remove it:
git worktree unlock feature-worktreegit worktree remove feature-worktreeYou can also add a reason for the lock:
git worktree lock --reason "Working on critical feature, do not remove" feature-worktreeCheck what’s locked:
git worktree list --porcelainMistake 5: Using the Same Path Name as a Branch Name
I tried to create a worktree with the same path as an existing branch:
git worktree add feature-auth feature-authGot this error:
fatal: 'feature-auth' already existsGit thinks you’re trying to create a worktree at the existing branch location. The path and branch name conflict.
How to Fix It
Use a different path name:
git worktree add ../auth-work feature-authOr specify the full path explicitly:
git worktree add -b feature-auth ../auth-feature mainThe pattern is:
git worktree add <path> <branch>Make the path different from the branch name to avoid confusion.
Quick Reference Table
| Error | Cause | Fix |
|---|---|---|
| ”contains modified files” | Uncommitted changes | Commit, stash, or use -f |
| ”already exists” | Path conflict | Use different path name |
| Stale references | Manual deletion | git worktree prune |
| Broken paths | Moved repo | git worktree repair |
| Accidental ops | No protection | git worktree lock |
Summary
Git worktree errors are frustrating but fixable. The five most common mistakes are:
- Removing with uncommitted changes - Commit, stash, or force
- Manually deleting directories - Use prune to clean references
- Moving the main repository - Run repair to fix paths
- Accidental operations - Use lock for protection
- Path name conflicts - Use different names for paths and branches
Remember the key commands: remove -f for forced removal, prune for cleanup, repair for path fixes, and lock/unlock for protection.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments