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)
bashgit branch -d feature/loginGit refuses if the branch has unmerged commits.
Force-delete a local branch (merged or not)
bashgit branch -D feature/abandoned-ideaUse with caution unmerged commits will be lost.
Delete a remote branch
bashgit push origin --delete feature/loginClean up stale remote-tracking refs locally
bashgit fetch --prune
# or during a regular fetch
git fetch -pRun 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.