Delete a branch you no longer need with a single command

A local branch is a copy of a project branch that exists only on your computer. When you finish working on a feature or fix, you delete the branch to keep your workspace clean. Git gives you two commands to do this: one that is safe and one that forces deletion even if the work is not fully saved.

The safe way is git branch -d branch-name. This deletes the branch only if you have already merged its changes into another branch (usually main). If you have unmerged work, Git stops you and warns you before anything disappears.

The forced way is git branch -D branch-name (capital D). Use this only when you are certain you want to throw away unmerged changes. Most of the time, the safe version is what you want.

Key Takeaways

  • Use git branch -d branch-name to delete a branch safely — Git will refuse if you have unmerged work.
  • Use git branch -D branch-name only when you are sure you want to discard unmerged changes permanently.
  • You cannot delete the branch you are currently on; switch to a different branch first with git checkout or git switch.
  • Deleting a local branch does not affect the branch on a remote server like GitHub — you delete those separately if needed.
  • You can see all your local branches and which one you are on by typing git branch with no arguments.

Check which branch you are currently on

Before you delete anything, find out which branch you are working in right now. Type this into your terminal or command prompt:

git branch

Git will show you a list of all your local branches. The one with an asterisk (*) next to it is the branch you are on. For example, you might see:

* main   feature-login   bugfix-header

This means you are currently on main. You cannot delete the branch you are standing on — Git will refuse. If the branch you want to delete is the one with the asterisk, move to a different branch first (usually main).

Switch to a different branch before deleting

If you are on the branch you want to delete, switch away first. Use either of these commands:

git checkout main

or

git switch main

Both do the same thing. git switch is newer and simpler; git checkout is older but still works everywhere. After you run one of these, type git branch again to confirm the asterisk moved to main.

In most projects, main is the stable branch where finished work lives. Switching there before you delete is the safest habit because you know that branch will not disappear.

Delete the branch safely with -d

Now that you are on a different branch, delete the one you no longer need:

git branch -d feature-login

Replace feature-login with the actual name of the branch you want to remove. If the branch has been merged into the branch you are on (or into main), Git deletes it silently and you are done.

If the branch has unmerged work, Git stops you with a message like:

error: The branch 'feature-login' is not fully merged. If you are sure you want to delete it, run 'git branch -D feature-login'.

This is Git protecting you. It means there are commits on that branch that are not yet in any other branch. Read the message carefully. If you want to keep that work, do not delete the branch — merge it first or create a backup. If you are certain the work is not needed, use the forced delete (see next section).

Force delete with -D when you are certain

If Git refuses to delete because of unmerged work, and you are absolutely sure you want to throw that work away, use capital D:

git branch -D feature-login

This bypasses all safety checks and deletes the branch when ready, even if it has commits that exist nowhere else. Once you run this command, that work is gone from your local machine. You cannot undo it with a straightforward command.

Only use -D when you are certain. If there is any doubt, use -d instead and let Git warn you. A warning is better than losing work by accident.

Delete a remote branch if you pushed it to GitHub or another server

Deleting a local branch only removes it from your computer. If you pushed that branch to a remote server like GitHub, it still exists there. Most teams delete the remote branch too to keep the server clean.

To delete a branch on a remote server, use:

git push origin --delete feature-login

Replace origin with the name of your remote (usually origin for GitHub) and feature-login with the branch name. This removes the branch from the server but does not affect your local copy — you delete that separately with git branch -d or git branch -D.

Many hosting platforms like GitHub also let you delete a remote branch through their web interface. After you merge a pull request, GitHub often offers a button to delete the branch automatically.

Recover a deleted branch if you made a mistake

If you deleted a branch and realized you needed it, there is still a way to recover it — but only if you act quickly. Git keeps a record of all branch movements for about 30 days in something called the reflog.

Type this to see the reflog:

git reflog

Look for the commit where the branch was deleted. It will show something like branch: Reset from [old-commit]. Copy the commit hash (the string of letters and numbers), then recreate the branch:

git branch feature-login [commit-hash]

Replace [commit-hash] with the actual hash you found. This recreates the branch pointing to that commit. After 30 days, Git cleans up the reflog and recovery becomes much harder, so act soon if you need to restore something.

Frequently Asked Questions

What happens if I delete a branch that has not been merged?

Git stops you and shows an error message when you use git branch -d. The branch is not deleted. If you use git branch -D instead, the branch is deleted and the unmerged commits are removed from your local machine. You can recover them from the reflog for about 30 days.

Can I delete the main branch?

Technically yes, but you should not. If you are on main, Git will refuse to delete it. If you switch to another branch first, git branch -d main will work. Most teams protect the main branch on the server side so it cannot be deleted by accident, even if you try.

Do I have to delete the remote branch if I delete the local one?

No. Deleting your local branch does not touch the remote. If you want to clean up the server too, run git push origin --delete branch-name separately. Many teams do this automatically after a pull request is merged.

What is the difference between git branch -d and git branch -D?

-d (lowercase) is safe — it refuses to delete if there is unmerged work. -D (uppercase) forces deletion no matter what. Use -d by default and only switch to -D when you are absolutely certain you want to discard unmerged changes.

Can I delete multiple branches at once?

Yes. You can list multiple branch names: git branch -d branch1 branch2 branch3. Git will delete all of them (or refuse all of them if any have unmerged work). For more control, delete them one at a time.