Delete a local and remote branch
Removes a branch from your local clone and from the shared remote in one step, after git first checks that it's actually safe to delete.
Last updated
git branch -d feature-branch && git push origin --delete feature-branch
How it works
git branch -d is the SAFE local delete: it checks whether feature-branch has been merged into your current branch and refuses with an error if it hasn't, protecting you from silently losing unmerged commits. git push origin --delete feature-branch then removes that same-named branch from the remote — local deletion never touches the remote on its own, so this second command is what actually clears it off GitHub/GitLab/etc.
Running both together (chained with &&) is the complete cleanup for a merged feature branch you no longer need locally or remotely, in one line instead of two separate steps to remember.
Watch out for
- →If -d refuses because the branch genuinely has unmerged work you're sure you want to discard, use -D (capital) to force the local delete — there's no confirmation prompt, so double-check first.
- →Deleting the remote branch doesn't clean up other people's clones — they'll still see the old remote-tracking ref until they run git fetch --prune, which is worth mentioning to collaborators after a shared branch is removed.
- →Double-check the branch name before running the remote delete — git push origin --delete doesn't ask for confirmation, and a typo or the wrong name silently targets whatever branch you actually typed.
Part by part
- git branch -d
- Delete a local branch, but only when it has been merged into its upstream or the current branch. The lowercase d is the safe form.
- &&
- Runs the second command only if the first succeeded, so a refused local delete stops the remote one from happening.
- git push origin --delete
- Deletes the branch on the remote. This is a separate operation — removing a local branch never touches the server.
Common mistakes
git branch -D feature-branchThe capital D forces deletion regardless of merge state, which is precisely the check worth keeping. Reach for it only after you have confirmed the work is somewhere else.
git branch -d feature-branchDeleting only locally leaves the remote branch in place, so it still appears for everyone else and in the repository's branch list. Both halves are needed to actually retire a branch.
git branch -d feature-branch && git push origin --delete feature-branchgit push origin :feature-branchThe old colon syntax still works and reads as a typo to anyone who has not seen it. --delete says what it does, which matters in a command that removes something from a shared server.
Questions
Why does git refuse to delete my branch?
Because -d checks whether the branch has been merged, and it compares against the upstream when one is set — not necessarily against the branch you are standing on. A squash-merged pull request often looks unmerged for this reason.
I deleted a branch by mistake. Can I get it back?
Usually. git reflog shows recent HEAD positions and git branch <name> <hash> recreates it from the commit. The commits survive until git garbage-collects them, which gives you a window of weeks.
How do I clean up branches deleted on the remote?
git fetch --prune removes the stale remote-tracking references. Set fetch.prune to true and it happens on every fetch, which stops the branch list filling with things that no longer exist.
Does deleting the branch delete its commits?
Not if they were merged — they are part of the target branch's history. Unmerged commits become unreachable and are eventually garbage-collected, which is what -d is protecting you from.