Why Should You Delete a Merged Feature Branch? — Local & Remote Deletion, Summarized
Isn't it fine to keep a branch around after merging?
Working with git, this thought comes up at some point.
"Isn't it harmless to just leave a merged branch alone? Maybe it's kept around to preserve history?"
I used to think that too. But as a project progresses, the output of git branch starts looking like this.
feature/v1-init
feature/v2-i18n
feature/v3-folder-management
feat/rename-folder-to-chain
fix/keyboard-issue
main
There's no way to tell at a glance which is still active work and which is an already-merged branch. This is where it starts getting annoying.
Why you should delete feature branches
1. History lives in commits, not branches
A branch is just a pointer to a specific commit. Once merged, those commits stay right there in main. Deleting the branch doesn't erase the work history.
git log --oneline main
# The merged commits are all still right there
2. You can't tell active work from finished work
As the branch list grows, confusion sets in: "is this branch still being worked on?" Especially confusing to future-you alone, and even more so on a team.
3. Remote branches are visible to everyone
Locally, only you need to look at it, but a branch pushed to origin piles up in every teammate's git branch -a list.
How to delete a branch
Deleting a local branch
git branch -d branch-name
The -d flag only deletes branches that are already merged. A safe option.
git branch -d feat/rename-folder-to-chain
# Deleted branch feat/rename-folder-to-chain (was 4f5e866)
To forcibly delete an unmerged branch, use -D (capital).
git branch -D branch-name # force delete, unmerged commits are lost — be careful
Deleting a remote branch
git push origin --delete branch-name
git push origin --delete feat/rename-folder-to-chain
# To https://github.com/...
# - [deleted] feat/rename-folder-to-chain
Deleting local + remote at once
git branch -d branch-name && git push origin --delete branch-name
Frequently used pattern summary
| Situation | Command |
|---|---|
| Delete a merged local branch | git branch -d branch-name |
| Force-delete an unmerged local branch | git branch -D branch-name |
| Delete a remote branch | git push origin --delete branch-name |
| List local branches | git branch |
| List local + remote branches | git branch -a |
| List already-merged branches | git branch --merged main |
Troubleshooting
"remote ref does not exist" error
error: unable to delete 'feat/something': remote ref does not exist
This shows up when the branch never existed remotely to begin with. If you worked entirely locally and never pushed, trying to remote-delete it produces this error. Just delete it locally instead.
Deleted it, but it still shows in git branch -a
Sometimes a remote branch deletion still lingers in the local cache. Clean it up with this.
git fetch --prune
# or, shorthand
git fetch -p
Summary
A merged feature branch should be deleted right away. The commit history stays in main, so there's nothing to worry about.
# Branch cleanup routine after a merge
git checkout main
git merge feat/my-feature
git push origin main
git branch -d feat/my-feature # delete locally
git push origin --delete feat/my-feature # delete remotely
A clean branch list makes it obvious at a glance what's currently being worked on. Deleting branches right when you merge, rather than cleaning them all up at once later, is a habit that's much easier to keep up with.
backtodev
A 40-something PM returns to code. Learning, failing, and growing.