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
- Delete a merged local branch:
git branch -d feature-old
- Force delete an unmerged branch:
git branch -D feature-old
- 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 --mergedto see branches that are safe to delete.
Common issues
- Deleting the wrong branch: use
git reflogto recover lost commits. - Remote deletion does not affect other developers' local copies.