How can I remove a Git branch locally?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Deleting a local Git branch is easy, but the safe command depends on whether the branch has already been merged. Git gives you a protected delete mode and a force delete mode because branch cleanup is really a question of whether you are discarding unique commits or just removing an unneeded name.
Switch Off the Branch First
Git will not let you delete the branch you are currently using. Start by moving to another branch, usually main or whatever branch you use as your local base.
On older Git versions, the equivalent command is:
Only after that should you try to remove the old branch.
Use git branch -d for Safe Deletion
If the branch has already been merged, use the safe form:
The lowercase -d tells Git to delete the local branch only if its tip is already reachable from another reference. In practice, that means Git refuses to delete work that looks unmerged.
This is the right default because it protects you from throwing away unfinished local history during routine cleanup.
Use git branch -D Only When You Intend to Discard Work
If the branch contains commits you no longer want, force deletion is available:
This bypasses the merge safety check. It is useful when:
- the branch was purely experimental
- the branch was merged elsewhere and the local history is disposable
- you recreated the work on another branch
- you explicitly want to drop the local tip
The important point is that -D is not a general-purpose shortcut. It is a discard operation.
Check Which Branches Are Already Merged
Before deleting several branches, inspect merge state.
This gives you a practical split between branches that are obvious cleanup candidates and branches that still carry unique work. A common local cleanup sequence is:
That sequence keeps your local view closer to the current repository state.
Local Deletion Is Not Remote Deletion
Deleting a local branch only removes the name from your local repository. It does not delete the remote branch on origin.
Local branch deletion:
Remote branch deletion:
Cleaning stale remote-tracking references:
These are separate operations, and mixing them up is one of the most common Git cleanup mistakes.
Recovery Is Often Possible
If you delete the wrong local branch, recovery is often straightforward with reflog.
Find the commit that used to be the branch tip, then recreate the branch name.
That is not a reason to be careless, but it does mean accidental local branch deletion is usually recoverable if you act soon.
Common Pitfalls
- Trying to delete the branch you are currently on.
- Using
-Dby habit and discarding commits you still needed. - Forgetting that local cleanup does not remove the remote branch.
- Deleting branches without checking whether they are already merged.
- Assuming a deleted branch is unrecoverable without checking
git reflogfirst.
Summary
- Switch to another branch before trying to delete the old one.
- Use
git branch -dfor the normal safe case. - Use
git branch -Donly when you intentionally want to discard unmerged local work. - Remote deletion and pruning are separate from local branch deletion.
- If you delete the wrong branch,
git reflogis usually the first recovery tool to try.
Related reading
- How can I remove a GitLab project?
- How can I remove an entry in global configuration with git config?
- How can I Remove .DS_Store files from a Git repository?
- How can I remove file from Git history?
- How can I remove/delete a large file from the commit history in the Git repository?
- How can I remove/delete a large file from the commit history in the Git repository?
- How can I rename a git stash?
- How can I rename a local Git branch?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.