CommandGit

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

Commandgit
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.

Related in Commands