brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git worktree prune cleans up worktrees you deleted by hand

Git
~/tips/tip-git-worktree-prune-stale

git worktree prune -n -v

Removes leftover bookkeeping for worktree folders that no longer exist.

If you delete a worktree folder with rm -rf instead of git worktree remove, git still thinks it exists. The branch then stays locked to a folder that’s gone.

git worktree prune -n -v
git worktree prune

In git worktree list, such a worktree is marked prunable. git worktree prune removes the administrative files git keeps for it in .git/worktrees. With -n -v it only tells you what it would remove, for example Removing worktrees/hotfix: gitdir file points to non-existent location.

The practical effect: as long as git thinks a worktree has a branch checked out, you can’t check out that branch anywhere else. After pruning, the branch is free again. Only the bookkeeping goes; the branch and its commits are untouched.

I’d rather use git worktree remove ../hotfix in the first place. It deletes the folder and the bookkeeping in one go, and it refuses when there are uncommitted changes, a nice safety net.

Don’t prune a worktree on a USB disk or network share that’s simply not mounted right now. To git it looks the same as a deleted folder. Lock those with git worktree lock and prune will leave them alone.

Further reading: git-worktree documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link