Delete a local branch with git branch -d

To remove a branch that exists only on your computer, use git branch -d followed by the branch name. For example, to delete a branch called feature-login, type:

git branch -d feature-login

Git will refuse to delete a branch if it contains commits that are not merged into your current branch. If you are certain you want to delete it anyway, use git branch -D (capital D) instead. This forces deletion without checking for unmerged work. The capital D is a safety override — use it only when you know the work is saved elsewhere or no longer needed.

Before deleting, switch to a different branch — you cannot delete the branch you are currently on. Use git checkout main or git switch main to move to another branch first. This is a hard requirement; Git will stop you if you try to delete your active branch.

Delete a remote branch with git push

To remove a branch from a remote server like GitHub or GitLab, use git push origin --delete branch-name. For the feature-login example:

git push origin --delete feature-login

This removes the branch from the remote repository but does not touch your local copy. If you want to delete both at once, delete the local branch first with git branch -d, then delete the remote branch with the command above. Many teams delete the remote branch after a pull request is merged, then each developer deletes their local copy when they finish working on that feature.

The remote name is usually origin if you cloned the repository from GitHub or another hosting service. If you use a different remote name, replace origin with that name. You can see all your remote names by typing git remote -v.

Key Takeaways

  • Use git branch -d branch-name to delete a branch on your computer, and switch to a different branch first because you cannot delete the branch you are currently on.
  • Use git branch -D (capital D) to force deletion if Git warns that the branch has unmerged commits, but only when you are certain the work is saved elsewhere.
  • Use git push origin --delete branch-name to remove a branch from a remote server like GitHub, and replace origin with your actual remote name if it differs.
  • Deleting a remote branch does not automatically delete your local copy, so you may need to run both commands if you want both gone.
  • Check which branches exist with git branch (local) or git branch -r (remote) before deleting to prevent accidental removal.

List branches before you delete

To see which branches you have locally, type git branch. The current branch will have an asterisk next to it. To see branches on the remote server, type git branch -r. This shows you the full list of what exists before you remove anything.

This step prevents accidental deletion. Once you know which branch you want to remove, you can proceed with confidence. If you have many branches, you can also type git branch | grep search-term to filter the list and find the exact branch name you need.

What happens after you delete a branch

Deleting a branch does not erase the commits it contained — they remain in Git's history if they were merged into another branch. If the commits were never merged, they become unreachable but Git keeps them for a time before garbage collection removes them permanently. This means your work is not lost immediately even if you delete by mistake.

If you delete a branch by mistake, you can usually recover it within a few weeks using git reflog, which shows a log of all branch movements. Type git reflog to see the history, find the commit hash where your branch pointed, then create a new branch at that commit with git branch branch-name commit-hash. This is a safety net for accidental deletions, though it works best if you act quickly.

Delete multiple branches at once

To delete several local branches in one command, list them after git branch -d:

git branch -d feature-login feature-signup feature-reset

For remote branches, you can delete multiple at once by repeating the --delete flag:

git push origin --delete feature-login feature-signup feature-reset

This saves time when cleaning up after finishing a set of related features. If one branch fails to delete (for example, because it has unmerged commits), the others will still be deleted, so check the output to see which ones succeeded.

Understand the difference between local and remote deletion

A local branch exists only on your computer in your repository folder. A remote branch exists on the server where you pushed your code — GitHub, GitLab, Bitbucket, or your own server. When you delete one, the other remains untouched.

Most workflows delete the remote branch first after a pull request is merged, because that signals to the team that the work is done. Developers then delete their local copy when they are ready to clean up their machine. This two-step process prevents accidental deletion and gives everyone time to grab the code if needed.

Frequently Asked Questions

Can I undo a branch deletion?

Yes, if the deletion was recent. Use git reflog to find the commit hash where the branch pointed, then create a new branch at that commit with git branch branch-name commit-hash. This works for local deletions within a few weeks before Git's garbage collection runs.

What does the error "branch not fully merged" mean?

It means the branch contains commits that do not exist in your current branch. Use git branch -D to delete anyway, or merge the branch first with git merge branch-name if you want to keep the work.

Do I need to delete the local branch if I delete the remote branch?

No. Deleting the remote branch only removes it from the server. Your local copy stays until you delete it separately with git branch -d. Many developers delete the remote branch after a pull request is merged, then delete the local branch when they are done working.

What is the difference between -d and -D?

The lowercase -d is safe — it refuses to delete if the branch has unmerged commits. The uppercase -D forces deletion without checking. Use -D only when you are certain the work is saved elsewhere or no longer needed.

How do I delete a branch if I do not know its exact name?

Type git branch to see all local branches, or git branch -r for remote branches. You can also use git branch | grep keyword to search for branches containing a specific word, which helps when you have many branches.