Skip to content

/commands/git/git-delete-remote-branch

git push origin --delete branch: delete a remote branch

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated
tool
Git
versions
Git 1.7+ (--delete), fetch.prune: Git 1.8.5+
os
linux, macos, windows

git push origin --delete feature deletes the branch feature on the remote origin. It does not touch your local branch: delete that with git branch -d feature (or -D if it is not merged). Other clones keep a stale origin/feature until they run git fetch --prune.

Delete remote and local branch

bash
git init -q --bare origin.git
git clone -q origin.git me 2>/dev/null && cd me
git switch -q -c main && echo a > a && git add a && git commit -qm init && git push -q -u origin main
git switch -q -c feature && echo b > b && git add b && git commit -qm feature && git push -q -u origin feature
git switch -q main && git merge -q feature
git push origin --delete feature
git branch -d feature
git branch -a

Output

To /tmp/work/origin.git
 - [deleted]         feature
Deleted branch feature (was e588cdd).
* main
  remotes/origin/main

git branch -d refuses to delete a branch that is not merged into its upstream or the current branch; -D forces it.

Clean up stale refs in other clones

bash
git init -q --bare origin.git
git clone -q origin.git a 2>/dev/null && cd a
git switch -q -c main && echo a > a && git add a && git commit -qm init && git push -q -u origin main
git push -q origin main:old-feature
cd .. && git clone -q -b main origin.git b && cd b && git branch -r
cd ../a && git push -q origin --delete old-feature
cd ../b && git fetch --prune
git branch -r

Output

  origin/main
  origin/old-feature
From /tmp/work/origin
 - [deleted]         (none)     -> origin/old-feature
  origin/main

To prune on every fetch, set git config fetch.prune true (add --global for all repositories).

Errors you may see

Deleting a branch that is not on the remote fails, for example when it was already deleted by someone else or the name has a typo:

bash
git init -q --bare origin.git
git clone -q origin.git me 2>/dev/null && cd me
git switch -q -c main && echo a > a && git add a && git commit -qm init && git push -q -u origin main
git push origin --delete no-such-branch

Output · exit 1

error: unable to delete 'no-such-branch': remote ref does not exist
error: failed to push some refs to '/tmp/work/origin.git'

Check the names that exist with git ls-remote --heads origin. Deleting a local branch that is not merged stops with a hint to use -D:

bash
git init -q -b main repo && cd repo
echo a > a && git add a && git commit -qm init
git switch -q -c experiment && echo b > b && git add b && git commit -qm wip
git switch -q main
git branch -d experiment

Output · exit 1

error: the branch 'experiment' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D experiment'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

Git also refuses to delete the branch you are on (or that is checked out in another worktree): switch to another branch first.

Command reference

GoalCommand
Delete a remote branchgit push origin --delete NAME
Same, older syntax (push nothing to it)git push origin :NAME
Delete a merged local branchgit branch -d NAME
Delete an unmerged local branchgit branch -D NAME
Remove stale remote-tracking refsgit fetch --prune (or git remote prune origin)
Delete a remote taggit push origin --delete v1.0.0

FAQ

Why does git branch -r still show a branch that was deleted on the remote?

Remote-tracking refs are only updated when you fetch, and a plain fetch does not remove refs for deleted branches. Run git fetch --prune, or set fetch.prune true.

Can I restore a remote branch I deleted by mistake?

If any clone still has the commits, push them back with git push origin COMMIT:refs/heads/NAME. The deletion output shows the old commit only for the local branch (Deleted branch NAME (was HASH)), so note it before you delete.

Does deleting a remote branch delete my local branch?

No. git push origin --delete changes only the remote. Delete the local branch separately with git branch -d NAME.

Sources