Delete a Git branch locally and on the remote

Delete local and remote branch refs separately, prune stale tracking refs, and check merge status and worktrees before removing a branch.

Best practices
Illustration of Git branches and version history

Delete a local branch with git branch -d branch-name. Delete the corresponding server branch with git push origin --delete branch-name. They are separate operations: removing one does not automatically remove the other.

Delete the local branch

First switch away from the branch you intend to delete:

git switch main
git branch -d feature-x

The -d check requires the branch to be merged into its configured upstream, or into HEAD if it has no upstream. That is not always the same as checking whether it has been merged into main. Review the intended integration branch explicitly when that distinction matters.

To remove a branch even though the merge check fails, use git branch -D feature-x only after preserving any commits you want. It removes the branch reference; it does not instantly erase every underlying commit object.

Delete the server branch

git push origin --delete feature-x

Confirm the remote and branch name. Server permissions or protection rules may reject deletion. Other collaborators retain their local branches and any stale remote-tracking refs until they update their own repositories.

Prune stale remote-tracking refs

# Fetch updates and remove stale tracking refs for origin.
git fetch origin --prune

# Or prune without fetching new objects.
git remote prune origin

Pruning origin/feature-x does not delete someone's local feature-x branch.

Review several merged branches

List exact branch names first:

git branch --merged main --format='%(refname:short)'

Review the list, then delete the selected names with git branch -d branch-one branch-two. Avoid piping a broad text filter straight into deletion: a substring filter for main can skip unrelated names, and branches checked out in linked worktrees need separate handling.

FAQ

What does “branch checked out at” mean?

The branch may be active in this checkout or another linked worktree. Use git worktree list, then switch that worktree to a different branch before deleting the reference.

Can a deleted branch be recovered?

If you know the commit ID and the object still exists, run git branch recovered COMMIT. Another branch or tag may still retain the commits. A relevant HEAD reflog can help locate a lost tip, but deleting a branch also deletes its branch reflog, and unreachable objects can eventually be pruned. Recovery is not guaranteed.

Does deleting a remote branch remove other people's work?

It removes the shared server ref. Existing clones retain their copies, but new fetches cannot retrieve that branch by name. Coordinate when others still use it.

See git-branch, git-push, git-fetch, and git-worktree.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting