Clean up merged Git branches and worktrees
Once a pull request is merged and the remote branch is deleted, the local copy sticks around. So does any worktree you made for it.
I wrote a little function called gone to clear up this space.
Run it in any repo and it removes every local branch whose remote branch no longer exists, along with its worktree. It will remove uncommitted work, but more on that below (and use at your own risk).
The function
Add this to your ~/.zshrc (or ~/.bashrc):
function gone() { git fetch --prune || return git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads | awk '$2=="[gone]"{print $1}' | while read -r b; do wt=$(git worktree list --porcelain | awk -v b="refs/heads/$b" ' /^worktree /{p=substr($0,10)} $1=="branch" && $2==b {print p}') [ -n "$wt" ] && git worktree remove --force "$wt" git branch -D "$b" done}Then reload your shell (source ~/.zshrc), cd into a repo, and type gone.
It reports any deleted branches:
$ goneDeleted branch feat-a (was 4a95cd0).Deleted branch feat-b (was 4a95cd0).How it works
Let’s walk through it one piece at a time.
1. Sync with the remote.
git fetch --prune || return--prune tells Git to forget any remote branches that were deleted on the server. That’s what lets Git notice a branch is “gone.”
The || return stops the function if the fetch fails (e.g., you’re offline). Without it, you’d be deleting branches based on old information.
2. List every local branch and its tracking status.
git for-each-ref --format='%(refname:short) %(upstream:track)' refs/headsThis prints one line per local branch: its name, then how it compares to the remote branch it tracks. The output looks like this:
feat-a [gone]feat-b [gone]keepmain[gone] means the branch used to track a remote branch, and that remote branch has since been deleted. Usually, that means the PR was merged.
Note: Branches you never pushed don't track anyhing, so they show nothing here. That means gone leaves them alone.
3. Filter to just the “gone branches.
awk '$2=="[gone]"{print $1}'awk splits each line on spaces. $1 is the branch name and $2 is the status. If the status is exactly [gone], print the name.
4. Find a worktree using the branch.
wt=$(git worktree list --porcelain | awk -v b="refs/heads/$b" ' /^worktree /{p=substr($0,10)} $1=="branch" && $2==b {print p}')Git won’t delete a branch that’s checked out in a worktree, so we need to remove the worktree first.
git worktree list --porcelain prints a script-friendly block for each worktree:
worktree /Users/chris/code/my-app-feat-bHEAD 4a95cd0...branch refs/heads/feat-bThe awk script remembers the path every time it sees a worktree line. substr($0,10) grabs everything after worktree (9 characters), so paths with spaces still work. When it hits a branch line that matches our branch, it prints the path it remembered.
5. Remove the worktree, then the branch.
[ -n "$wt" ] && git worktree remove --force "$wt"git branch -D "$b"If a worktree was found, remove it. Then delete the branch.
Why the force flags
Both commands use force, and that’s on purpose.
git branch -D(capital D) deletes the branch even if Git thinks it isn’t merged. If your team squash-merges or rebase-merges PRs, Git can’t tell your branch was merged, so a plain-dwould refuse every time. (That means if you don’t squash merge or rebase merge, you should definitely switch this to the lowercase-d. It’s a risk I’m willing to accept knowing how I work with Git locally.)git worktree remove --forceremoves the worktree even if it has uncommitted changes or untracked files.
Again, please be careful here. Anything uncommitted in a gone branch’s worktree is deleted. If you sometimes leave experiments lying around in old worktrees, drop the --force and Git will stop and tell you instead.
Note: If you're currently on a gone branch in your main checkout, Git won't remove that worktree or delete the branch. You'll see an error for that one branch and the rest still get cleaned up. Switch to main and run it again.