How to delete a branch in Git

· Category: Git

Short answer

Use git branch -d <branch> for safe deletion or -D for force deletion of unmerged branches.

Steps

  1. Delete a merged local branch:
git branch -d feature-old
  1. Force delete an unmerged branch:
git branch -D feature-old
  1. Delete a remote branch:
git push origin --delete feature-old

Tips

  • Always switch to another branch before deleting the current one.
  • Prune stale remote branches with git fetch --prune.
  • Use git branch --merged to see branches that are safe to delete.

Common issues

  • Deleting the wrong branch: use git reflog to recover lost commits.
  • Remote deletion does not affect other developers' local copies.