Skip to content
LatchkeyLatchkey home

git worktree remove: Usage, Options & Common CI Errors

git worktree remove cleanly deletes a linked working tree and its bookkeeping.

After a parallel build finishes, remove tears down the extra worktree properly - unlike rm -rf, it also clears the repo-side metadata so listings stay accurate.

What it does

git worktree remove deletes the working directory of a linked tree and its administrative files under .git/worktrees, provided the tree is clean. It refuses to remove the main worktree.

Common usage

Terminal
git worktree remove ../feature
git worktree remove --force ../feature   # even if dirty
git worktree prune                       # clean up stale entries
git worktree prune -n                    # dry run

Options

Flag / subcommandWhat it does
<path>Remove the worktree at path
-f / --forceRemove even with local changes (twice if locked)
pruneDrop metadata for already-deleted trees
prune -nShow what prune would remove

Common errors in CI

fatal: '<path>' contains modified or untracked files, use --force to delete it - the tree is dirty; pass --force when you are sure. A locked worktree needs --force twice. If you deleted the directory with rm instead, the entry lingers until git worktree prune.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git worktree remove: Usage, Options & Common CI Errors?
After a parallel build finishes, remove tears down the extra worktree properly - unlike rm -rf, it also clears the repo-side metadata so listings stay accurate.
What it does?
git worktree remove deletes the working directory of a linked tree and its administrative files under .git/worktrees, provided the tree is clean. It refuses to remove the main worktree.
Common errors in CI?
fatal: '<path>' contains modified or untracked files, use --force to delete it - the tree is dirty; pass --force when you are sure. A locked worktree needs --force twice. If you deleted the directory with rm instead, the entry lingers until git worktree prune.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card