How to Delete Git Branch Locally and Remotely
Step-by-step guide to delete a Git branch both locally and remotely. Use git push origin --delete, git branch -d bugfix, and git fetch --prune to remove branches and prevent reappearing after git pull. Fix common errors.
How do I delete a Git branch both locally and remotely?
I’ve attempted to delete a remote branch with the following commands:
$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.
$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.
$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).
After running these commands, I tried to push and pull:
$ git push Everything up-to-date $ git pull From github.com:gituser/gitproject * [new branch] bugfix -> origin/bugfix Already up-to-date.
How do I properly delete the remotes/origin/bugfix branch both locally and remotely?
To delete a Git branch both locally and remotely, first remove the branch on the server with git push origin --delete bugfix (or the older git push origin :bugfix), then delete your local branch with git branch -d bugfix (use -D to force), and finally run git fetch --prune (or git remote prune origin) so the remotes/origin/bugfix tracking ref is cleaned up. Your earlier git branch -rd origin/bugfix only deleted the local remote‑tracking ref — it didn’t remove the branch on the remote, so git pull fetched it back. Verify with git branch -a and git ls-remote --heads origin after the steps.
Contents
- Quick checklist: commands to delete a Git branch
- Delete Git branch remotely (safe and force)
- Delete Git branch locally (safe and force)
- Why remotes/origin/bugfix reappeared
- Verify and prune across clones
- Troubleshooting & edge cases
- Recover a deleted branch
- Sources
- Conclusion
Quick checklist: commands to delete a Git branch
If you want the short, copy‑and‑paste workflow:
# 1) Delete the remote branch on the server (recommended)
git push origin --delete bugfix
# OR (old syntax)
git push origin :bugfix
# 2) Switch off the branch, then delete the local branch (safe or force)
git switch main # or `git checkout main`
git branch -d bugfix # safe: refuses if unmerged
git branch -D bugfix # force: deletes even if unmerged
# 3) Remove stale remote-tracking refs so remotes/origin/bugfix is gone locally
git fetch --prune
# or
git remote prune origin
# or for all remotes
git fetch --all --prune
# 4) Verify
git branch -a
git branch -r
git ls-remote --heads origin
That sequence is the standard “git delete branch” and “git delete remote branch” workflow.
Delete Git branch remotely (safe and force)
The server-side branch must be deleted on the remote—local branch commands don’t do it. Use:
- Recommended (clear):
git push origin --delete bugfix - Older equivalent:
git push origin :bugfix
What this does: the first form tells the remote to remove the refs/heads/bugfix ref. The colon syntax pushes an empty source to the remote branch, which also deletes it. See practical notes and examples in the Git Tower guide and tutorials like freeCodeCamp for the same pattern: How can I delete a remote branch in Git? and How to Delete a Git Branch Both Locally and Remotely.
Common gotchas:
- If
git push origin --delete bugfixerrors with “remote ref not found”, the branch may already be gone on the server or the branch name differs. Check withgit ls-remote --heads originorgit branch -r. - If deletion is blocked, the branch may be protected (GitHub/GitLab settings) or you lack permission. Delete via the repository web UI or ask an admin if needed.
- Running plain
git pushwithout--deletewon’t remove the remote branch; that’s why yourgit pushsaid “Everything up-to-date”.
Delete Git branch locally (safe and force)
Before deleting any local branch, make sure you’re not currently on it.
Switch away:
git switch main # or git checkout main
Delete safely (refuses if branch hasn’t been merged into HEAD):
git branch -d bugfix
Force delete (drops unmerged changes):
git branch -D bugfix
Notes:
- Use
git branch -dto avoid losing work unintentionally. If you really want to discard an unmerged branch,-Dforces the delete. - If
git branch -dcomplains the branch is not fully merged, consider merging or creating a backup branch first.
For background and alternatives, see DataCamp and GeeksforGeeks coverage of local deletion and force options: https://www.datacamp.com/tutorial/git-delete-branch and https://www.geeksforgeeks.org/git/delete-a-git-branch-locally-and-remotely/.
Why remotes/origin/bugfix reappeared (what went wrong)
You deleted the local remote-tracking ref with git branch -rd origin/bugfix — that removes the local copy of remotes/origin/bugfix. But the actual branch still existed on the server, so the next git pull (which does a fetch) re-created the tracking ref from the remote. In short: deleting a remote-tracking branch locally is not the same as deleting the branch on the remote server.
Stack Overflow explains this exact pitfall and the proper order: delete the remote branch on the server first, then clean up local tracking refs so the branch doesn’t come back on fetch/pull: https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely.
So the fix for your situation is:
- Remove the branch on the remote:
git push origin --delete bugfix. - Clean local tracking refs:
git fetch --prune(orgit remote prune origin). - Remove your local branch if you still have it:
git branch -d bugfixor-D.
Verify and prune across clones (prune, fetch, check)
After remote deletion, prune stale refs locally:
git fetch --prune
# or, explicitly:
git remote prune origin
# verify:
git branch -r # show remote-tracking branches
git branch -a # show local + remote
git ls-remote --heads origin
If teammates still see the branch, they must also run git fetch --prune (or git remote prune origin) in their clones. Remote-tracking refs are local metadata; they stick around until each clone prunes or fetches with prune.
If you maintain CI bots or automation that re-creates branches, the branch will come back after deletion. Confirm whether any automation recreates bugfix after you delete it.
GeeksforGeeks and DataCamp walk through the prune/verify steps in more detail: https://www.geeksforgeeks.org/git/delete-a-git-branch-locally-and-remotely/ and https://www.datacamp.com/tutorial/git-delete-branch.
Troubleshooting & edge cases
- Protected branch: GitHub/GitLab may block deletion. Resolve in the repo settings or use a user with the right permissions.
- Wrong remote name: if your remote isn’t
origin, use that remote name (git remote -vto check). - Typo in branch name: confirm exact name with
git branch -r | grep bugfixorgit ls-remote --heads origin. - Branch recreated by someone else or CI: check
git log origin/bugfixfor recent pushes and the author. - Can’t delete current branch: you must switch to another branch first.
git push origin --deletereturns “Everything up-to-date”: that message appears when you rangit pushwithout deletion flags. Re-run with--delete.- If you only want to remove the local tracking ref and not touch the remote branch,
git branch -rd origin/bugfixis fine — but remember pull/fetch will restore it if the remote branch still exists.
Recover a deleted branch
If you accidentally deleted a branch and need to recover it:
- Find the tip commit (use reflog or look through logs):
git reflog # find the commit SHA that was tip of bugfix
git log --all --grep 'commit message or pattern'
- Recreate the branch from the SHA:
git checkout -b bugfix <sha>
- Push it back to the remote:
git push -u origin bugfix
Simple and fast. Reflog usually retains the commits for a while unless garbage collection has removed them.
Sources
- How do I delete a Git branch locally and remotely? — Stack Overflow
- How to Delete a Git Branch Both Locally and Remotely — freeCodeCamp
- Delete a Git Branch Locally and Remotely — GeeksforGeeks
- Git Delete Branch: How to Remove Local and Remote Branches — DataCamp
- How can I delete a remote branch in Git? — Git Tower
Conclusion
In short: run git push origin --delete bugfix to remove the branch from the server, delete your local branch with git branch -d bugfix (or -D), and run git fetch --prune so remotes/origin/bugfix disappears locally. That order — remote delete, local delete, then prune — is the reliable “git delete branch” and “git delete remote branch” workflow that prevents the branch from reappearing on pull.