Michael Boutin

Delete local and remote git branches

bashMay 10, 2026

Delete branches one at a time, or sweep up local branches whose upstream has already been deleted.

To delete a single branch, run one of these:

terminal
# Local
git branch -d BRANCH_NAME
 
# Remote
git push origin -d BRANCH_NAME

Bulk-clean stale local branches

After merging PRs, the remote branches get deleted but their local copies stick around. Start by pruning your remote-tracking refs so git knows which upstreams are gone:

terminal
git fetch --prune

List local branches whose upstream is now gone:

terminal
git branch -vv | grep ': gone' | awk '{print $1}'

Once the list looks right, pipe it into git branch -D to delete them in one shot:

terminal
git branch -vv | grep ': gone' | awk '{print $1}' | xargs -r git branch -D

-D force-deletes, which is what you want here since merged squash/rebase branches often don't look merged to your local git.

Liked this? Get the next one in your inbox.

One email every other Tuesday. Engineering, product, and what I'm shipping. Unsubscribe anytime.

No spam. See the privacy policy.

  • bash

    Change a git remote's URL

    Point an existing remote at a new URL without removing and re-adding it.

    May 10, 2026
  • bash

    Remove node_modules from a git repo

    Stop tracking a committed node_modules folder and ignore it going forward.

    May 10, 2026
  • bash

    Stop tracking a file that's already committed

    Adding a path to .gitignore does not untrack files git already knows about. Use git rm --cached to drop it from the index while keeping the local copy.

    May 6, 2026