How do I delete a Git branch locally and remotely?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Deleting a Git branch is simple once you separate the two cases: local branch deletion and remote branch deletion. The commands are different because one removes a branch reference in your repository, while the other asks a remote server to remove its branch reference.
Delete a Local Branch
For a local branch that has already been merged, use:
The -d flag is the safe form. Git refuses to delete the branch if it still contains commits that are not fully merged into the current branch or its configured upstream.
If you really want to delete it regardless of merge status:
-D is the force-delete form. Use it carefully because it can hide work you have not merged elsewhere.
You Cannot Delete the Branch You Are On
If you are currently checked out on the branch you want to remove, switch away first:
Git will not let you delete the branch that is currently checked out, because that branch is actively attached to your working tree.
Delete a Remote Branch
To delete a branch from the remote named origin, use:
That is the modern and most readable syntax. It tells the remote server to remove its branch reference.
There is also an older equivalent form:
It still works, but --delete is clearer and easier to remember.
Local and Remote Are Independent
Deleting a local branch does not delete the remote branch. Deleting the remote branch does not automatically remove your local branch either.
That is why a full cleanup often looks like:
These are two different operations on two different repositories.
Clean Up Stale Remote-Tracking References
After a remote branch is deleted, your local repository may still keep an outdated remote-tracking reference until you fetch or prune.
To clean that up:
Or:
This removes stale references such as origin/feature/login after the branch no longer exists on the server.
Example Workflow
A typical sequence after merging a feature branch is:
This leaves your local repository and your remote branch list cleaner and less confusing for future work.
When to Use -d Versus -D
Use -d when:
- the branch has been merged,
- or you want Git to protect you from deleting unmerged work.
Use -D when:
- the branch is experimental and you want to discard it,
- or you know the commits are preserved somewhere else and the safety check is not useful.
In most day-to-day workflows, -d should be the default.
Check Before Deleting
If you are unsure whether the branch has been merged, inspect it first:
The second command shows commits reachable from feature/login that are not on main. If that list is non-empty, deleting the branch may hide work you still need.
Common Pitfalls
The biggest pitfall is using -D reflexively instead of the safer -d. Force deletion is fine when intentional, but it should not be the default habit.
Another mistake is thinking local and remote branch deletion are the same operation. They are not. You often need to do both.
Developers also sometimes delete the remote branch and forget to prune, leaving stale remote-tracking names that make the branch list look inconsistent.
Finally, do not delete the current branch and then wonder why Git refuses. Switch to another branch first.
Summary
- Delete a local branch with
git branch -d branch_name. - Force-delete a local branch with
git branch -D branch_name. - Delete a remote branch with
git push origin --delete branch_name. - Local and remote deletions are separate operations.
- Use
git fetch --pruneto clean up stale remote-tracking references afterward.
Related reading
- How do I revert a Git repository to a previous commit?
- How do I undo the most recent local commits in Git?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I make Git forget about a file that was tracked, but is now in .gitignore?
- How to modify existing, unpushed commit messages?
- How and/or why is merging in Git better than in SVN?
- How are Spring Data repositories actually implemented?
.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.