Black Friday sale: 30% off Pro and Unlimited, until 22 December

How to clean up old Git worktrees and merged branches

List your Git worktrees, remove the ones you’ve finished with, and check which branches are merged before you delete them.

A finished task can leave a worktree folder and a branch behind. Start with the folder, then check whether the branch has anything you still need.

If you’re still moving between tasks, see how to start a fix in a separate worktree while your edits stay open.

Need to keep a worktree but put it somewhere else? See how to move a Git worktree.

List your worktrees

Run this from your main checkout. In this example it’s ~/code/acme/web.

git worktree list
~/code/acme/web               25d710f [main]
~/code/acme/web-checkout-fix  2da99ea [checkout-fix]
~/code/acme/web-hotfix        25d710f [hotfix]
~/code/acme/web-old-spike     bb4585b [old-spike]

The first line is the main checkout. Each line shows a path, a commit and the branch checked out there. If you see (detached HEAD), save any commits you need on a branch before removing that worktree. Here is how. If Git says a branch is already used by a worktree, find the checkout.

Remove a worktree you’re done with

Check for changes and local files before removing the worktree.

git -C ../web-checkout-fix status --short --ignored --untracked-files=all
!! local-cache.txt

!! marks an ignored file. Git can delete ignored files when it removes a worktree. Commit work you want to keep or move the files out of the worktree before continuing.

git worktree remove ../web-checkout-fix

That removes the folder and leaves the branch in place. If the worktree is locked, check why before unlocking it with git worktree unlock ../web-checkout-fix.

Inspect each worktree in turn. This one has a file you might want to keep.

git -C ../web-old-spike status --short --ignored --untracked-files=all
?? notes.txt

?? marks an untracked file. Move notes.txt somewhere safe if you need it. Check the status again. Once the worktree is clean and you’ve saved any ignored files you need, remove it.

git worktree remove ../web-old-spike

Prune records for folders that are already gone

If you deleted a worktree folder in Finder, Git can still have a record of it. Look for prunable in the worktree list.

~/code/acme/web-hotfix     25d710f [hotfix] prunable

Preview which records Git would clear right now.

git worktree prune --dry-run --verbose --expire now
Removing worktrees/web-hotfix: gitdir file points to non-existent location

Check that the listed paths are really gone. A worktree on a disconnected drive can also look missing. When the list is right, use the same expiry to clear the records. The branches stay in place.

git worktree prune --expire now

Check which branches are merged

git branch --merged main
  checkout-fix
  hotfix
* main

The tip of each listed branch is in the history of main. The * marks your current branch. If you see a +, the branch is checked out in another worktree. Switch that worktree to another branch or remove it before deleting the branch.

Delete a finished branch from the list with lowercase -d.

git branch -d checkout-fix
Deleted branch checkout-fix (was 2da99ea).

Use the merged list to decide what to delete. For branches whose remote copy is gone, see how to delete merged and stale branches. The -d guard checks the branch’s upstream if it has one, or your current commit otherwise. A branch can pass that guard even when its commits are still outside main. See which branches still need a look.

git branch --no-merged main

Check squash merges separately

A squash merge puts the changes into a new commit. The original branch tip can stay outside main, so it may be missing from the merged list and -d may refuse. Confirm the pull request landed and check that it included the branch’s latest changes. If you’re sure you can drop the branch, git branch -D squash-demo bypasses the merge check.

Clean up remote branches separately

To check whether hotfix is merged on the remote, fetch its tip and the tip of main from origin. This leaves local tags in place.

git fetch --no-prune --no-tags --refmap= origin \
  '+refs/heads/main:refs/remotes/origin/main' \
  '+refs/heads/hotfix:refs/remotes/origin/hotfix' &&
git branch -r --merged origin/main --list origin/hotfix

Continue only if the command prints origin/hotfix. That means its tip is in the history of origin/main. If your team is done with the branch, run these lines together to delete it from origin.

tip=$(git rev-parse --verify refs/remotes/origin/hotfix) &&
git merge-base --is-ancestor "$tip" refs/remotes/origin/main &&
git push --force-with-lease="refs/heads/hotfix:$tip" \
  origin --delete refs/heads/hotfix

The merge check uses the exact commit saved in tip. If it’s outside origin/main, the commands stop before the push. If the remote branch has moved since you saved its tip, Git refuses the deletion. Fetch again and review the new tip.

Check for unpushed work before cleanup

push& (Pushand) shows where commits are waiting to be pushed across your repositories and worktrees, so you can review them before cleaning up.

Review and remove worktrees in the app with Pro or higher, or during the 14-day Unlimited trial. Free includes pushing in three repositories you choose.

More Git how-tos