Skip to content
WorktreeWise
Git Worktree CommandStep #5

git worktree lock: Protect a Git Worktree from Pruning

The git worktree lock command marks a worktree as locked, preventing it from being accidentally deleted by automated cleanup routines or pruned by git worktree prune.

Basic Syntax & Lock Reason

Terminal
# Lock a worktree against automatic pruning
$git worktree lock <worktree>

You can also annotate the lock with an explanatory message using --reason:

Terminal
# Lock with custom explanation
$git worktree lock --reason "Stored on external SSD during benchmark testing" ../perf-benchmarks
📁
Normal Worktree

Pruneable if directory is temporarily unreachable

git worktree lock →← git worktree unlock
🔒
Locked Worktree

Safe on external SSDs, USB drives, or persistent build caches

When Should You Lock a Git Worktree?

1. Removable & External Media

If your worktree folder is located on a portable SSD, flash drive, or network share that gets disconnected, Git might consider it "missing" and prune its administrative records during automatic garbage collection. Locking protects it while unmounted.

2. Long-Running Workflows

If you have persistent build caches, background tests, or Docker volumes mounted into a worktree, locking guarantees team members or automated cleanup scripts won't accidentally delete the working tree.

Inspecting Locked State

Running git worktree list displays the locked badge and reason:

git worktree list output with lock
/home/developer/projects/my-app          9a8b7c6 [main]
/home/developer/projects/perf-benchmarks 1d2e3f4 [perf/bench] (locked: "Stored on external SSD during benchmark testing")
WorktreeWise

Lock Worktree

Try Free

WorktreeWise lets you toggle lock protection with a single switch on any worktree card. You can attach custom lock reasons and visually distinguish protected worktrees at a glance.

Lock a Git worktree against deletion in WorktreeWise

Related commands

When you are ready to resume normal operations or clean up the directory, release the lock using git worktree unlock.