Command guide

How to Delete a Git Branch, Locally and on the Remote

By Admin · Updated · 4 min read · Checked against the official Git docs

Delete a local branch with git branch -d branch-name, and a remote branch with git push origin --delete branch-name. They’re separate operations: deleting one never touches the other.

Quick answer

git switch main
git branch -d feature/old            # local
git push origin --delete feature/old # remote
Delete a Git branch locally with git branch -d and remotely with git push --delete

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 18 November 2026, and the output shown is copied from the terminal. Your commit hashes will differ.

Delete a local branch

-d is the safe version. It refuses to delete a branch whose commits aren’t merged anywhere, so you can’t lose work by accident:

$ git branch -d feature/old
error: the branch 'feature/old' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/old'

That message is Git protecting you. If you’re sure the work isn’t needed, use capital -D:

$ git branch -D feature/old
Deleted branch feature/old (was 4224ded).

Note the hash in the output (was …). Copy it somewhere if there’s any chance you’ll want the branch back.

You also can’t delete the branch you’re currently on:

$ git branch -d main
error: cannot delete branch 'main' used by worktree at '/home/sam/shop-site'

Switch to another branch first (git switch main), then delete.

Delete a remote branch

$ git push origin --delete feature/old
To /home/sam/remote/shop-site.git
 - [deleted]         feature/old

The older syntax git push origin :feature/old (colon in front) does the same thing; you’ll still see it in old answers. If the branch is already gone, for example because GitHub deleted it automatically after a pull request was merged, you’ll get this:

$ git push origin --delete feature/old
error: unable to delete 'feature/old': remote ref does not exist
error: failed to push some refs to '/home/sam/remote/shop-site.git'

That’s harmless. Your clone just has an outdated list of remote branches. Clean it up with git fetch --prune, which removes every remote-tracking branch that no longer exists on the server. To have Git do that on every fetch, run git config --global fetch.prune true.

Delete it everywhere

For a finished feature branch, the full cleanup is three commands:

git switch main
git branch -d feature/old
git push origin --delete feature/old

Teammates then run git fetch --prune to drop it from their lists. Their own local copies of the branch (if they checked it out) stay until they delete them too.

Delete all merged branches at once

After a few months a repo collects dozens of dead branches. List the ones already merged into main, then delete them:

$ git branch --merged main
  feature/a
  feature/b
  feature/c
* main
$ git branch --merged main | grep -vE '^\*|^\s*main$' | xargs git branch -d
Deleted branch feature/a (was b874d35).
Deleted branch feature/b (was 1dfc7b1).
Deleted branch feature/c (was 701066e).

The grep keeps main and your current branch (marked *) out of the list. Run the first command alone first and read the list before piping it into git branch -d. Because it uses lowercase -d, anything unmerged that slips through still gets refused.

Recover a branch you deleted by mistake

Deleting a branch deletes the label, not the commits. If you were on that branch recently, the reflog still knows where it pointed:

$ git branch -D feature/x
Deleted branch feature/x (was 139a972).
$ git reflog -3
3d22fab HEAD@{0}: checkout: moving from feature/x to main
139a972 HEAD@{1}: commit: Work
3d22fab HEAD@{2}: checkout: moving from main to feature/x
$ git branch feature/x HEAD@{1}

Here HEAD@{1} was the last commit on the deleted branch, and git branch feature/x HEAD@{1} recreates it right there. If you have the hash from the “Deleted branch … (was 6f7a548)” message, git branch feature/x 6f7a548 works too. If the branch still exists on the remote, it’s even simpler: git switch feature/x recreates it from origin/feature/x.

FAQ

What is the difference between git branch -d and -D?

-d only deletes a branch whose commits are merged into its upstream or your current branch. -D deletes it regardless, which can discard unmerged commits.

Does deleting a local branch delete it on GitHub?

No. Local and remote branches are separate. Run git push origin --delete branch-name to remove it from GitHub.

Why does git say “remote ref does not exist”?

The branch was already deleted on the remote, often automatically after a merged pull request. Run git fetch --prune to update your list of remote branches.

Can I recover a deleted Git branch?

Usually, yes. Find the last commit in git reflog or the hash Git printed when deleting, then run git branch name <hash>.

Written by Admin

Writes practical Git guides for Git Commands. Every command is checked against the official Git documentation.

About Git Commands