How to Delete a Git Branch Safely

Remove a local branch with git branch -d, or delete a branch from a remote with git push. This guide explains the safe commands, why Git may refuse a deletion, and how to avoid losing commits that have not been merged.

Practice Git workflows →

Delete a local Git branch

Switch to a different branch first; Git will not delete the branch you currently have checked out. Then use the safe delete option:

git switch main
git branch -d feature-name

Replace main with the branch you want to keep, and feature-name with the branch to remove. The -d option asks Git to check that the branch has been merged before deleting it.

Delete a remote branch

To delete a branch from a remote named origin, push a deletion to that remote:

git push origin --delete feature-name

This changes the shared remote repository. Check with your team and confirm you have selected the correct remote and branch before running the command. A branch protected by hosting-service rules may not be deletable until those rules are changed by an authorized maintainer.

What is the difference between -d and -D?

git branch -d is the safer option: it refuses to delete a branch if Git believes its commits have not been merged. git branch -D forces deletion even if the branch contains unmerged work.

Before using -D, inspect commits that are not on your current branch:

git log --oneline main..feature-name

If the output includes commits you may need, preserve the branch or save its work before deleting it. A forced local deletion does not remove those commits from other branches or remotes, but it does remove the branch reference that made them easy to find.

Why can’t I delete my Git branch?

“Cannot delete branch checked out”

Switch to another branch or create a replacement branch before deleting the current one. Git requires a working branch to remain checked out in a normal repository.

“Branch is not fully merged”

Git found commits it cannot confirm are merged into the current branch. Compare the branch histories and verify the work is merged or safely saved before considering -D. For help integrating changes, see the Git merge vs. rebase guide.

Remote branch still appears after deletion

Your local remote-tracking reference may be stale, or another user may have recreated the branch. Fetch with pruning to remove local references for branches that no longer exist on the remote:

git fetch --prune origin

Check whether a branch is merged before deleting it

List local branches and see which ones Git considers merged into your current branch:

git branch --merged
git branch --no-merged

A branch marked merged is merged into the branch you currently have checked out—not necessarily into your team’s default branch. Switch to the intended target branch before relying on this list.

Frequently asked questions

Does deleting a branch delete its commits?

Deleting a branch removes its name, or reference. Commits reachable from another branch remain available there. Commits that become unreachable may eventually be removed by Git’s cleanup, so preserve unmerged work before deleting its only reference.

Does deleting a local branch delete the remote branch too?

No. Local and remote branches are separate. Use git push origin --delete branch-name to request removal from a remote.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →