Quick Answer: To delete a Git branch locally, run git branch -d branch-name (use -D to force). To delete it on the remote, run git push origin --delete branch-name. You can’t delete the branch you’re currently on — switch to another branch first.
Delete a Local Branch
git branch -d branch-name # safe delete (only if merged)
git branch -D branch-name # force delete (even if not merged)-d(lowercase) is the safe option — Git refuses if the branch has unmerged changes.-D(uppercase) force-deletes — use it when you’re sure you don’t need the work.
Delete a Remote Branch
git push origin --delete branch-name
# Older equivalent syntax:
git push origin :branch-nameThis removes the branch from the remote (e.g., GitHub, GitLab). origin is the default remote name — change it if yours is different.
Delete a Branch Both Locally and Remotely
Deleting a local branch does not remove it from the remote, and vice versa. To fully remove a branch, run both commands:
git branch -d branch-name # delete locally
git push origin --delete branch-name # delete on the remoteClean Up Stale Remote-Tracking Branches
After a branch is deleted on the remote, your local copy may still show a stale origin/branch-name reference. Prune them:
git fetch --prune
# or
git remote prune originDelete Multiple Branches at Once
# Delete several local branches
git branch -d feature-1 feature-2 feature-3
# Delete all local branches already merged into main
git branch --merged main | grep -v '\*\|main' | xargs -r git branch -dVerify the Branch Is Gone
git branch # list local branches
git branch -r # list remote-tracking branches
git branch -a # list all branchesSee our full guides on listing remote branches and the complete Git commands reference.
Common Errors & Fixes
- “error: The branch ‘x’ is not fully merged.” — Git is protecting unmerged work. Merge it first, or force-delete with
git branch -D xif you’re sure. - “error: Cannot delete branch ‘x’ checked out at …” — you’re on that branch. Switch away first:
git switch main(orgit checkout main), then delete. - “remote ref does not exist” — the remote branch is already gone; run
git fetch --pruneto sync. - Deleted the wrong branch? — recover it if you know the commit:
git checkout -b branch-name <commit-hash>(find it withgit reflog).
Command Cheat Sheet
| Task | Command |
|---|---|
| Delete local (safe) | git branch -d name |
| Delete local (force) | git branch -D name |
| Delete remote | git push origin --delete name |
| Prune stale refs | git fetch --prune |
| List branches | git branch -a |
Frequently Asked Questions
How do I delete a local Git branch?
Run git branch -d branch-name for a safe delete, or git branch -D branch-name to force-delete an unmerged branch.
How do I delete a remote Git branch?
Run git push origin --delete branch-name. This removes the branch from the remote repository (e.g., GitHub or GitLab).
What is the difference between git branch -d and -D?
-d only deletes a branch that has been merged (safe). -D force-deletes a branch even if it has unmerged commits.
Why can’t I delete my current branch?
Git won’t delete the branch you’re checked out on. Switch to another branch first with git switch main, then delete it.
