remote
local
git-branch
git

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.

Browse interview questions

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:

bash
git branch -d feature/login

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:

bash
git branch -D feature/login

-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:

bash
git switch main
git branch -d feature/login

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:

bash
git push origin --delete feature/login

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:

bash
git push origin :feature/login

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:

bash
git branch -d feature/login
git push origin --delete feature/login

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:

bash
git fetch --prune

Or:

bash
git remote prune origin

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:

bash
1git switch main
2git pull
3git branch -d feature/login
4git push origin --delete feature/login
5git fetch --prune

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:

bash
git branch --merged
git log --oneline main..feature/login

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 --prune to clean up stale remote-tracking references afterward.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.