How do you delete a branch in Git?

Delete a local branch with git branch -d and a remote branch with git push origin --delete.

GitVersion Control

Overview

After merging a feature branch you typically want to clean it up. Deleting a local branch and its corresponding remote branch are two separate commands.

Key Points

  • •`git branch -d` deletes a fully-merged local branch safely.
  • •`git branch -D` force-deletes even if the branch has unmerged commits.
  • •`git push origin --delete <name>` deletes the branch on the remote.
  • •You cannot delete the branch you are currently on switch away first.
  • •After deleting a remote branch, other team members need `git fetch --prune` to remove stale tracking refs.

Examples

Delete a local branch (safe only if merged)

bash
git branch -d feature/login

Git refuses if the branch has unmerged commits.

Force-delete a local branch (merged or not)

bash
git branch -D feature/abandoned-idea

Use with caution unmerged commits will be lost.

Delete a remote branch

bash
git push origin --delete feature/login

Clean up stale remote-tracking refs locally

bash
git fetch --prune
# or during a regular fetch
git fetch -p

Run this after deleting remote branches so `git branch -r` stays tidy.

Pro Tip

GitHub and GitLab can be configured to auto-delete the source branch when a PR/MR is merged. That keeps the remote clean without any extra commands.