Delete a local branch with git branch -d

To delete a branch that exists only on your computer, use git branch -d followed by the branch name. For example, to delete a branch called feature-login, type:

git branch -d feature-login

Git will refuse to delete a branch if it contains commits that haven't been merged into another branch. If you're certain you want to delete it anyway — even though those commits will be lost — use a capital D instead: git branch -D feature-login. The lowercase -d is safer because it protects you from accidentally losing work.

Before you delete, make sure you're not currently on that branch. If you are, switch to a different branch first by typing git checkout main (or whatever your main branch is called), then delete the one you want to remove.

Delete a branch that's stored on a server

If you pushed your branch to a remote server — usually called origin — deleting it locally doesn't remove it from the server. You need a separate command to delete the remote version.

Type git push origin --delete feature-login to remove the branch from the server. Replace origin with the actual name of your remote if it's different, and feature-login with the branch name you want to remove.

Some older Git documentation shows git push origin :feature-login (with a colon), which does the same thing. Both work, but the --delete version is clearer about what you're doing.

Delete both at once

If you want to clean up completely, delete the local branch and the remote branch in sequence. First delete locally with git branch -d feature-login, then delete the remote copy with git push origin --delete feature-login.

The order doesn't matter — you can delete remote first, then local, or the other way around. Git will tell you if something goes wrong, like if the branch doesn't exist or if you're trying to delete a branch that hasn't been merged.

What happens to commits when you delete a branch

Deleting a branch doesn't automatically delete the commits on that branch. If those commits have been merged into another branch (like main), they stay in your repository history even after the branch is gone. You can still see them in the commit log of the branch they were merged into.

If the commits have not been merged anywhere, they become orphaned. Git keeps them for about 30 days in the reflog before cleaning them up. If you delete a branch by accident and realize it within a few days, you can usually recover it — ask Git to show you the reflog with git reflog and look for the branch name or commit hash.

Deleting multiple branches at once

If you have several branches to delete, you can remove them one at a time, or use a pattern to delete multiple branches that match a name. For example, git branch -d feature-* deletes all branches that start with feature-. Be careful with this — it will delete every matching branch, so double-check the pattern first.

You can also list all branches and delete them selectively. Type git branch to see all local branches, then delete the ones you want one by one. For remote branches, type git branch -r to see what's on the server.

Common mistakes and how to avoid them

The most common mistake is trying to delete a branch while you're still on it. Git will refuse with an error message. Switch to a different branch first — usually git checkout main works — then delete the branch you wanted to remove.

Another mistake is forgetting that deleting the local branch doesn't touch the remote. If you delete locally but not on the server, the branch still exists online and will show up when someone else pulls. Always delete both unless you have a reason to keep one or the other.

If you use git branch -D (capital D) on a branch with unmerged commits, those commits are gone unless you recover them from the reflog within 30 days. Use lowercase -d first to let Git warn you if there's unmerged work.

Key Takeaways

  • Use git branch -d branch-name to delete a branch on your computer; Git will warn you if the branch has unmerged commits.
  • Use git push origin --delete branch-name to remove a branch from the remote server where you pushed it.
  • Make sure you're not currently on the branch you want to delete — switch to a different branch first with git checkout main.
  • Deleting a branch doesn't erase commits that were merged into other branches, but unmerged commits become orphaned and will be cleaned up after about 30 days.
  • Use git branch -D (capital D) only if you're certain you want to delete unmerged work; lowercase -d is safer because it stops you from losing commits by accident.

Frequently Asked Questions

Can I undo a branch deletion?

If you deleted a branch by accident, you can usually recover it within 30 days using the reflog. Type git reflog to see a list of recent changes, find the commit hash or branch name, then type git checkout -b branch-name commit-hash to recreate the branch at that point. After 30 days, Git cleans up orphaned commits and recovery becomes much harder.

What's the difference between -d and -D?

The lowercase -d tells Git to delete the branch only if its commits have been merged elsewhere. The capital -D deletes it no matter what. Use -d by default — it protects you from losing unmerged work. Use -D only when you're absolutely sure you want to discard commits that haven't been merged into another branch.

Do I have to delete a branch after merging it?

No, but it's good practice. Deleting branches you've finished with keeps your branch list clean and makes it easier to see what work is actually in progress. The commits stay in your history even after the branch is deleted, so you don't lose anything by cleaning up.

How do I delete a branch on GitHub or GitLab without using the command line?

Most Git hosting sites have a web interface for deleting branches. On GitHub, go to the repository, click the Branches tab, find the branch you want to delete, and click the trash icon next to it. GitLab and Bitbucket have similar buttons. This deletes only the remote branch — you still need to delete the local copy on your computer if you want to clean up completely.

What if I get an error saying the branch doesn't exist?

This usually means you're trying to delete a branch that's already gone, or you typed the name wrong. Type git branch to see all local branches and git branch -r to see remote branches, then check the exact spelling and try again.