Delete a local branch with git branch -d

To delete a local branch, open your terminal or command prompt and run git branch -d branch-name, replacing branch-name with the actual name of the branch you want to remove. Git will delete the branch from your computer only — it does not touch the remote repository or affect anyone else's work.

Before you delete, make sure you are not currently on that branch. If you are, Git will refuse to delete it and show an error. Switch to a different branch first by running git checkout main (or whatever your primary branch is called), then run the delete command.

The -d flag is safe because Git will warn you if the branch contains work that has not been merged into another branch. If you see a warning, you have the chance to reconsider before losing anything.

Key Takeaways

  • Use git branch -d branch-name to delete a local branch from your computer only.
  • You must be on a different branch before you can delete the one you are working on.
  • Git warns you if the branch has unmerged work, giving you a chance to save it before deletion.
  • Use git branch -D (capital D) only if you are certain you want to discard unmerged work without a warning.
  • Deleting a local branch does not affect the remote repository or other people's copies of the project.

Switch away from the branch before deleting

Git prevents you from deleting a branch while you are actively working on it. This is a safety feature — you cannot delete the branch you are currently checked out to.

To move to a different branch, run git checkout main or git checkout develop, depending on which branch your project uses as the primary one. Once you are on that branch, you can delete the one you just left.

If you are unsure which branch you are on, run git branch with no arguments. Git will list all your local branches and put an asterisk next to the one you are currently on.

Understand the difference between -d and -D

The -d flag (lowercase) is the standard way to delete a branch. It checks whether the branch has been fully merged into another branch. If it has not, Git shows a warning and refuses to delete it. This prevents you from accidentally losing work.

The -D flag (uppercase) forces deletion without any warning, even if the branch contains unmerged commits. Use this only when you are absolutely certain you want to discard the work on that branch. Most of the time, -d is the right choice.

If -d refuses to delete a branch and you know the work is saved elsewhere or no longer needed, then -D is appropriate. But pause and confirm before using it.

Delete multiple branches at once

If you have several branches to clean up, you can delete more than one in a single command. Run git branch -d branch-one branch-two branch-three, listing each branch name separated by a space.

Git will process each deletion in order and warn you about any branch that contains unmerged work. If one branch fails the safety check, the others will still be deleted, so use this approach only when you are confident about all the branches you are removing.

What happens to the remote branch

Deleting a local branch does not delete the branch on the remote repository (such as GitHub or GitLab). The remote copy remains untouched, and other team members still have access to it.

If you want to remove the branch from the remote as well, that is a separate step. After deleting the local branch, you can run git push origin --delete branch-name to remove it from the remote. This is useful when you have finished a feature and want to clean up both your computer and the shared repository.

Recover a deleted branch if you made a mistake

If you delete a branch and then realize you needed it, Git keeps a record of recent actions. Run git reflog to see a history of where your HEAD has been. Find the commit where the branch pointed, note its hash (the long string of characters), and run git checkout -b recovered-branch-name commit-hash to recreate the branch at that point.

This works only if you have not run garbage collection or performed other cleanup operations. The sooner you realize the mistake, the better your chances of recovery. If you are unsure, ask a team member to check the remote repository — the branch may still exist there.

Frequently Asked Questions

What if Git says the branch is not fully merged?

This means the branch contains commits that do not exist on any other branch. If the work is important, switch to that branch, review the commits with git log, and decide whether to merge them elsewhere first. If the work is no longer needed, use git branch -D to force deletion.

Can I delete a branch that someone else is using?

Deleting your local copy does not affect anyone else — they have their own local copy on their computer. If you want to prevent others from using the branch, delete it from the remote with git push origin --delete branch-name, but let your team know first so they can save any work they need.

Does deleting a branch delete the commits on it?

No. If the branch was merged into another branch, the commits remain part of that branch's history. If the branch was never merged, the commits become unreferenced but are not when ready deleted — you can recover them with git reflog for a period of time.

How do I delete all branches except main?

There is no single command for this, but you can list all branches with git branch, then delete each one individually with git branch -d. Some developers write a shell script to automate this, but it is safer to delete branches one at a time so you can catch mistakes.