Delete a branch with git branch -d locally and git push origin --delete on the remote, then prune stale remote-tracking branches so they stop showing up.
The short answer
#Local branches and remote branches are separate, so you delete them separately. Use git branch -d for your local copy and git push origin --delete for the branch on the server.
_10git switch main_10git branch -d feature/login # local_10git push origin --delete feature/login # remote
Why it happens
#A Git branch is just a named pointer to a commit. Your repository has its own branches, the remote (usually origin) has its own, and Git also keeps remote-tracking branches such as origin/feature/login, which are your local record of where the remote's branches were the last time you fetched.
That gives you three things with similar names:
feature/login: your local branch.feature/loginon the server: the branch other people see.origin/feature/login: your cached copy of the server's pointer.
Deleting one does not delete the others. That is why a branch can disappear on GitHub and still show up in git branch -a on your machine.
Deleting a branch does not delete commits. If the work was merged, the commits are still reachable from main. If it was not merged, the commits become unreachable and Git will eventually clean them up, though git reflog can usually recover them for a while.
How to fix it
#Delete the local branch
You cannot delete the branch you are on, so switch away first:
_10git switch main_10git branch -d feature/login
-d is the safe option. It refuses if the branch has commits that are not merged into its upstream or into your current branch, with an error like "The branch 'feature/login' is not fully merged". If you are sure you want to throw that work away, use the capital form:
_10git branch -D feature/login
A branch that was squash merged or rebase merged on GitHub will often trigger the "not fully merged" error, because the commits on main have different hashes. Check that the pull request was merged, then use -D.
Delete the remote branch
_10git push origin --delete feature/login
You may also see the older form, which pushes "nothing" to the branch:
_10git push origin :feature/login
Both do the same thing. You need permission to push to the repository, and protected branches cannot be deleted this way. Many teams also turn on the host's option to delete the branch automatically when a pull request is merged.
Clean up stale remote-tracking branches
When someone else deletes a branch on the server, your origin/... copy hangs around until you prune it:
_10git fetch --prune
To make this happen on every fetch and pull:
_10git config --global fetch.prune true
Delete local branches that are already merged
To list local branches merged into main:
_10git branch --merged main
Check the list, then delete the ones you no longer need. Be careful with one-liners that pipe this into git branch -d; make sure they exclude main and any other long-lived branches.
Find branches whose remote has gone
After pruning, git branch -vv marks local branches whose upstream was deleted with gone:
_10git fetch --prune_10git branch -vv | grep ': gone]'
Those are usually safe to delete with -d or, for squash merged work, -D.
Common pitfalls
#- Trying to delete the branch you are currently on. Switch first.
- Running
git branch -d origin/feature/login. That is a remote-tracking branch, not a local one; prune it withgit fetch --prune, or delete the real remote branch withgit push origin --delete. - Reaching for
-Dbefore checking why-drefused. The warning is there to stop you losing unmerged work. - Deleting a remote branch that has an open pull request. Most hosts close the pull request when its branch disappears.
- Expecting the branch to vanish for teammates straight away. They will keep their local copies and stale
origin/...entries until they delete and prune them.
Related reading
#Was this resource helpful?