Delete a branch locally after you've merged it
Once you've merged a branch into your main branch and no longer need it, you can remove it from your computer. The command is straightforward: open your terminal or command prompt, make sure you're not currently on the branch you want to delete, then type git branch -d branch-name (replace "branch-name" with the actual name of your branch).
Git will only let you delete a branch this way if it's already been merged somewhere. This is a safety feature — it prevents you from accidentally losing work. If you try to delete a branch that has commits not yet merged, Git will refuse and show you an error message telling you the branch hasn't been fully merged.
For example, if you created a branch called "fix-login-button" and merged it into main, you would type git branch -d fix-login-button. The branch disappears from your local list, but the work itself stays in your main branch because it was merged there.
Key Takeaways
- Use git branch -d branch-name to delete a branch after it's been merged into another branch.
- Git prevents accidental deletion by refusing to remove branches that contain unmerged work.
- Deleting a local branch does not affect the branch on a remote server like GitHub — you must delete it separately there.
- If you need to delete a branch that has unmerged commits, use git branch -D (capital D) only if you're certain the work is saved elsewhere.
Force delete a branch with unmerged changes
Sometimes you have a branch with commits that were never merged, and you want to remove it anyway — perhaps the work was abandoned or saved in a different way. In this case, use git branch -D branch-name (capital D instead of lowercase d).
The capital D flag tells Git to delete the branch even if it contains work that hasn't been merged. Use this only when you're certain the commits are either saved elsewhere or no longer needed. There's no undo button after this command runs, so double-check the branch name before you press Enter.
Delete a branch from a remote server
If you pushed your branch to GitHub, GitLab, or another remote server, deleting it locally doesn't remove it from there. You need a separate command to delete the remote version: git push origin --delete branch-name.
Replace "origin" with the name of your remote if it's different (most projects use "origin" by default). This command tells the remote server to delete the branch. After you run it, the branch is gone both locally and on the server, and other people on your team won't see it anymore.
Some platforms like GitHub also let you delete a branch through the web interface after you've merged a pull request — you'll see a button offering to delete the branch right there on the page.
Check which branches still exist
Before and after deleting branches, you can see what you have. Type git branch to list all branches on your computer. The branch you're currently on will have an asterisk next to it. This is useful for confirming a branch is gone after deletion, or for double-checking the exact name before you delete.
To see branches on the remote server, type git branch -r. This shows you what exists on GitHub or wherever you pushed your work. If you deleted a branch locally but it still shows up in the remote list, you know you still need to run the push command to remove it from the server.
Why delete branches at all
Deleting old branches keeps your repository clean and makes it easier to find the branches you're actually working on. A project with dozens of merged branches clutters the branch list and can confuse new team members about what's active and what's finished.
Merged branches are safe to delete because their commits live on in the main branch (or whatever branch they were merged into). Deleting the branch is just removing a label — the work itself isn't lost. This is different from deleting commits, which is destructive and rare.
Undo an accidental deletion
If you deleted a branch and when ready realized you needed it, there's still a way to recover it. Git keeps a record of recent actions in the reflog. Type git reflog to see a list of recent changes, find the commit where the branch was deleted, and type git branch branch-name commit-hash to recreate the branch pointing to that commit.
This works best if you act quickly — Git keeps the reflog for about 30 days by default. If you wait months, the record may be gone. For this reason, it's worth taking a moment to confirm the branch name before you delete, rather than relying on recovery later.
Frequently Asked Questions
Will deleting a branch lose my work?
No, if the branch was merged. The commits stay in the branch it was merged into. If the branch was never merged, the commits are lost unless you recover them from the reflog within about 30 days.
What's the difference between -d and -D?
The lowercase -d flag is safe — it refuses to delete a branch with unmerged commits. The capital -D flag forces deletion even if commits aren't merged. Use -D only when you're certain the work is saved or no longer needed.
Do I have to delete the remote branch if I delete the local one?
No, they're separate. Deleting locally doesn't touch the remote server. You must run git push origin --delete branch-name to remove it from GitHub or another remote. Many teams do both at once to keep everything clean.
Can I delete the main branch?
Git will let you delete any branch, but most remote servers like GitHub prevent deletion of the default branch (usually main or master) as a safety measure. If you try, the server will refuse the request.
How do I know if a branch is safe to delete?
Check if it's been merged by running git branch --merged. Branches in that list are safe to delete with the -d flag. Branches not in the list have unmerged commits and require -D if you want to delete them.