How to Delete a Git Branch Both Locally and Remotely
How to Delete a Git Branch Both Locally and Remotely

How to Delete a Git Branch Both Locally and Remotely

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-name

This 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 remote

Clean 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 origin

Delete 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 -d

Verify the Branch Is Gone

git branch          # list local branches
git branch -r       # list remote-tracking branches
git branch -a       # list all branches

See 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 x if you’re sure.
  • “error: Cannot delete branch ‘x’ checked out at …” — you’re on that branch. Switch away first: git switch main (or git checkout main), then delete.
  • “remote ref does not exist” — the remote branch is already gone; run git fetch --prune to sync.
  • Deleted the wrong branch? — recover it if you know the commit: git checkout -b branch-name <commit-hash> (find it with git reflog).

Command Cheat Sheet

TaskCommand
Delete local (safe)git branch -d name
Delete local (force)git branch -D name
Delete remotegit push origin --delete name
Prune stale refsgit fetch --prune
List branchesgit 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.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *