/commands/git/git-delete-remote-branch
git push origin --delete branch: delete a remote branch
- 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
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 -aOutput
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
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 -rOutput
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:
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-branchOutput · 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:
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 experimentOutput · 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
| Goal | Command |
|---|---|
| Delete a remote branch | git push origin --delete NAME |
| Same, older syntax (push nothing to it) | git push origin :NAME |
| Delete a merged local branch | git branch -d NAME |
| Delete an unmerged local branch | git branch -D NAME |
| Remove stale remote-tracking refs | git fetch --prune (or git remote prune origin) |
| Delete a remote tag | git 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.