This error occurs when Git prevents operations on a worktree because it has been explicitly locked. Unlock the worktree using 'git worktree unlock' or use --force flags for remove/move operations.
The "fatal: 'worktree' is locked" error indicates that Git is preventing you from performing certain operations (like removing, moving, or pruning) on a linked worktree because it has been locked. Worktree locking is a deliberate safety mechanism in Git to protect working trees that might be stored on removable media or network shares that aren't always mounted. When a worktree is locked, Git refuses to: - Remove the worktree with `git worktree remove` - Move the worktree with `git worktree move` - Automatically prune the worktree's administrative files - Add a new worktree at the same path This protection exists because if Git pruned or removed a worktree while the storage was temporarily unavailable, you could lose work or end up with a corrupted repository state when the storage comes back online. The lock is stored as a file in the main repository's `.git/worktrees/<name>/locked` file. This file may optionally contain a reason string explaining why the worktree was locked, which can be helpful when deciding whether it's safe to unlock.
First, identify which worktrees are locked and examine their status:
# List all worktrees with verbose output
git worktree list --verboseThe output will show each worktree with its status. Locked worktrees will display "(locked)" after their path:
/home/user/project abc1234 [main]
/home/user/project-feature def5678 [feature] (locked)The verbose flag also displays the lock reason if one was provided when locking.
If you want to allow operations on the worktree, unlock it with:
# Unlock by worktree path
git worktree unlock /path/to/worktree
# Or unlock by the worktree directory name relative to main repo
git worktree unlock ../project-featureAfter unlocking, you can perform remove, move, or prune operations on the worktree.
# Verify the worktree is now unlocked
git worktree listIf you want to remove a locked worktree without unlocking it first, use the --force flag twice:
# Force remove a locked worktree
git worktree remove --force --force /path/to/worktree
# Or using shorthand
git worktree remove -f -f /path/to/worktreeWarning: Using double --force bypasses both the lock check and any uncommitted changes check. Make sure you've saved any important work before using this option.
To move a locked worktree to a new location without unlocking, use double --force:
# Force move a locked worktree
git worktree move --force --force /old/path /new/path
# Or using shorthand
git worktree move -f -f /old/path /new/pathThis is useful when you need to relocate a worktree from a failing drive or unmounted network share.
If you're trying to create a new worktree at a path that's still registered as locked (perhaps from a deleted worktree), use double --force:
# Force add worktree at a path that's locked
git worktree add --force --force /path/to/worktree feature-branchBefore doing this, make sure the original worktree at that path is truly gone and not just temporarily unavailable.
If the standard unlock command doesn't work, you can manually remove the lock file from the main repository:
# Navigate to your main repository
cd /path/to/main/repo
# List the worktrees directory
ls -la .git/worktrees/
# Find and remove the lock file for the specific worktree
rm .git/worktrees/<worktree-name>/lockedReplace <worktree-name> with the directory name of your worktree (usually the final component of the worktree path).
# Example: if worktree was at /home/user/project-feature
rm .git/worktrees/project-feature/lockedAfter removing the lock file, run git worktree list to verify the worktree is no longer locked.
If you've manually deleted a worktree directory but Git still tracks it, prune the stale entries:
# Dry run to see what would be pruned
git worktree prune --dry-run
# Actually prune stale worktree administrative files
git worktree pruneNote: git worktree prune will NOT remove locked worktrees, even if their directories are missing. You must unlock or force-remove them first.
# To see verbose output during prune
git worktree prune --verboseBefore unlocking, check if there was a reason provided for the lock:
# Check the lock file content directly
cat .git/worktrees/<worktree-name>/lockedIf the file exists but is empty, no reason was given. If it contains text, that's the reason the worktree was locked (e.g., "On network share - do not prune").
When to keep a worktree locked:
- It's on a USB drive or external storage that's sometimes disconnected
- It's on a network share that may be temporarily unavailable
- You want to prevent accidental deletion during cleanup operations
Lock a worktree with a reason:
git worktree lock --reason "On external backup drive" /path/to/worktreeHow worktree locking works internally:
Git stores worktree metadata in .git/worktrees/<name>/ within your main repository. When you lock a worktree with git worktree lock, Git creates a file called locked in that directory. The file's contents are the lock reason (if provided) or empty.
Automating worktree management in scripts:
When writing scripts that manage worktrees, handle locks gracefully:
#!/bin/bash
WORKTREE_PATH="/path/to/worktree"
# Check if worktree is locked before attempting removal
if git worktree list | grep -q "$WORKTREE_PATH.*locked"; then
echo "Worktree is locked, unlocking..."
git worktree unlock "$WORKTREE_PATH"
fi
git worktree remove "$WORKTREE_PATH"Worktree corruption recovery:
If your main repository's .git/worktrees directory becomes corrupted:
1. Back up the main repository
2. List the contents: ls -la .git/worktrees/
3. For each stale entry, check if the worktree directory still exists
4. Remove stale administrative directories: rm -rf .git/worktrees/<stale-name>
5. Re-register valid worktrees: git worktree add <path> <branch>
Git LFS and worktrees:
Some Git LFS post-checkout hooks can interfere with worktree operations. If you encounter lock-related issues with Git LFS, check GitHub issue git-lfs/git-lfs#2848 for workarounds.
Best practices for worktree locks:
- Always provide a --reason when locking worktrees so future-you knows why
- Document locked worktrees in your project's README or contributing guide
- Periodically audit locked worktrees: git worktree list | grep locked
- In CI/CD, avoid locking worktrees unless absolutely necessary
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server