How to Delete a Git Branch (Local and Remote)
The commands to delete a Git branch on your computer and on GitHub: git branch -d vs -D, git push origin --delete, cleaning up stale remote-tracking branches with fetch --prune, deleting merged branches in bulk, and recovering a branch you deleted by mistake.
Branches pile up. Once a feature is merged, its branch is clutter. Deleting one is quick — but there are two places a branch lives, and deleting it in one doesn't delete it in the other. (Git branches explained)
Delete a local branch
git branch -d feature/login
-d only works if the branch has been merged; otherwise Git stops you:
error: the branch 'feature/login' is not fully merged.
That's a safety check. If you're sure you don't want the unmerged work, force it with a capital D:
git branch -D feature/login
You can't delete the branch you're currently on — switch first:
git switch main
git branch -d feature/login
Delete a remote branch (on GitHub)
git push origin --delete feature/login
Or on GitHub: open the Branches page and click the bin icon, or click Delete branch on a merged pull request. You can also turn on Automatically delete head branches in the repository's settings, so merged PR branches disappear on their own.
Clean up stale remote-tracking branches
After a branch is deleted on GitHub, your computer still remembers it as origin/feature/login until you tell it to forget:
git fetch --prune
Make that automatic:
git config --global fetch.prune true
Delete all merged branches at once
List branches already merged into main:
git branch --merged main
Delete them all except main (macOS/Linux/Git Bash):
git branch --merged main | grep -v -E '^\*|^\s*main$' | xargs git branch -d
Look at the list before deleting, especially if you keep long-lived branches like develop.
Branches merged with squash merge don't show as merged (the commits differ), so they won't appear in --merged. Delete those individually with -D once you've confirmed the PR merged. (How to squash commits)
Recover a branch you deleted by mistake
The commits aren't gone straight away. Find the last commit of the branch:
git reflog
Look for the last entry on that branch, then recreate it:
git branch feature/login a1b2c3d
When you delete a branch, Git also prints its last commit: Deleted branch feature/login (was a1b2c3d). — that hash is all you need. (How to undo almost anything in Git)
If it was deleted on GitHub after a PR, the PR page has a Restore branch button.
Quick reference
| Task | Command |
|---|---|
| Delete merged local branch | git branch -d name |
| Force-delete local branch | git branch -D name |
| Delete remote branch | git push origin --delete name |
| Forget deleted remote branches | git fetch --prune |
| List merged branches | git branch --merged main |
| Restore a deleted branch | git branch name <commit> |
EasySpawn keeps your repository on a persistent server where Claude Code can tidy up merged branches for you — and the reflog is still there if it tidies too much. See how it works or join the waitlist.
Related: Git Branches Explained · How to Undo Almost Anything in Git · git fetch vs git pull · What Is a Pull Request?
Keep reading
zsh vs bash: What's the Difference and Which Should You Use?
bash and zsh are both shells — the programs that read your terminal commands. Why macOS switched to zsh, the differences you'll actually notice (config files, completion, globbing, arrays), Oh My Zsh, and why scripts should usually still be written for bash.
What Is WSL? Running Linux on Windows, Explained
WSL lets you run a real Linux system inside Windows without dual-booting or a separate virtual machine to manage. What it is, why web developers use it, how to install it, where your files live, and the performance and networking gotchas.