Delete a local branch with git branch -d
To remove a branch that exists only on your own computer, use git branch -d followed by the branch name. If you have a branch called fix-login-bug, the command is:
git branch -d fix-login-bug
Git will refuse to delete a branch if it contains work that has not been merged into your current branch. This is a safety feature — it prevents you from accidentally losing code. If you are certain you want to delete it anyway, use a capital D instead: git branch -D fix-login-bug. The capital D forces the deletion without checking.
Before you delete, make sure you are not currently on that branch. If you are, switch to a different branch first — usually main or master — by typing git checkout main.
Key Takeaways
- Use git branch -d branch-name to delete a local branch safely; Git will warn you if the branch has unmerged work.
- Use git branch -D branch-name (capital D) to force deletion without warnings, but only when you are certain the branch is no longer needed.
- You cannot delete a branch while you are currently on it; switch to a different branch first with git checkout main.
- To delete a branch on a shared server (remote), use git push origin --delete branch-name or git push origin :branch-name.
- Deleting a branch does not erase the work if it has been merged; merged code stays in the branches that contain it.
Delete a remote branch that others can see
If you pushed a branch to a shared server — usually called origin — deleting it from your computer does not remove it from the server. Other people can still see it and pull it down. To delete it from the server, use:
git push origin --delete branch-name
An older syntax that does the same thing is git push origin :branch-name (note the colon with nothing before it). Both work; the first is clearer to read.
You need permission to delete branches on the shared server. On most teams, anyone can delete their own branches, but deleting someone else's branch may require administrator access. Check with your team lead or repository owner if the command fails.
What happens when you delete a branch
Deleting a branch removes only the label that points to a set of commits. The actual work — the code changes — does not disappear if that work has been merged into another branch. For example, if you merged fix-login-bug into main, then deleted fix-login-bug, the code changes stay in main forever.
If you delete a branch that was never merged, the commits on that branch become unreachable from any branch label. Git keeps them for a while (usually 30 days) in case you change your mind, but they will eventually be cleaned up. This is why Git warns you before deleting an unmerged branch.
Deleting a branch does not affect any work that has already been pushed to the server and merged. It only removes the pointer to that branch, making it harder to find old work — but the work itself remains safe in the branches that contain it.
When to delete a branch
Delete a branch once its work has been merged and reviewed. On most teams, this happens after a pull request is approved and the code is combined into main or master. Keeping old branches around makes the list of branches cluttered and confusing, especially on large projects where dozens of people are working at once.
Some teams set up automation to delete branches automatically after a pull request is merged. If your team uses GitHub, GitLab, or Bitbucket, you can turn on this setting in the repository configuration so you do not have to remember to delete branches by hand.
Do not delete a branch if you are unsure whether its work has been merged. If you are not certain, ask a team member or check the pull request history first. It takes a few seconds to verify, and it prevents losing work.
Recover a deleted branch if you made a mistake
If you deleted a branch by accident, you can usually recover it within 30 days. Git keeps a log of all branch movements in a file called the reflog. To see what branches you have deleted recently, type:
git reflog
Look for an entry that shows the branch being deleted. It will have a message like branch: Reset to origin/branch-name or update ref. Copy the commit hash (the string of letters and numbers) from that line, then create a new branch pointing to that commit:
git checkout -b recovered-branch-name commit-hash
This recreates the branch at the point where it was deleted. If you need to push it back to the server, use git push origin recovered-branch-name. The reflog only works on your local computer; if you deleted a branch from the server and no one else has it, you cannot recover it from the server's reflog.
Common mistakes when deleting branches
The most common error is trying to delete a branch while you are currently on it. Git will refuse with a message like error: Cannot delete branch 'branch-name' checked out at. The fix is straightforward: switch to a different branch first with git checkout main, then delete the branch you wanted to remove.
Another mistake is using git branch -D (capital D) without thinking. The capital D skips the safety check and deletes when ready, even if the branch has unmerged work. Use the lowercase git branch -d first; if Git refuses, it is telling you something is wrong. Read the warning, investigate, and only use capital D if you are absolutely certain.
A third mistake is deleting a branch from your computer but forgetting to delete it from the server. This leaves a stale branch that confuses other team members. Always delete both the local and remote versions if the work is truly done.
Frequently Asked Questions
Can I delete a branch that someone else is still working on?
You can delete your local copy, but you should not delete the remote branch without talking to them first. If they are still pushing to that branch, deleting it from the server will cause problems when they try to push. Check with your team before deleting a shared branch.
What is the difference between git branch -d and git branch -D?
The lowercase -d is safe; it refuses to delete a branch unless its work has been merged. The capital -D forces deletion without checking. Use lowercase first. Only use capital D when you are certain the branch is no longer needed and you understand what you are deleting.
If I delete a branch, does the code disappear?
No, not if the code has been merged. Merged code stays in the branches that contain it. Deleting a branch only removes the label pointing to that work. If the branch was never merged, the commits become unreachable but Git keeps them for about 30 days before cleanup.
How do I delete multiple branches at once?
You can list multiple branch names: git branch -d branch1 branch2 branch3. For remote branches, use git push origin --delete branch1 branch2 branch3. This is faster than deleting one at a time on large projects.
Do I need to delete a branch if I am the only person using the repository?
No, but it is still a good habit. Deleting old branches keeps your branch list clean and makes it easier to find the branches you are actually working on. Even in a personal project, a tidy workspace saves time later.