1 minute read

Context

Branches are cheap—but abandoned branches add noise.

Over time, repositories accumulate:

  • merged feature branches
  • stale experiment branches
  • branches tied to completed tickets

Cleaning them up improves clarity and reduces mistakes, especially in fast-moving repos.


Deleting a Local Branch

To delete a local branch that has already been merged:

git branch -d my-branch

The -d flag is safe by default. Git will refuse to delete the branch if it hasn’t been fully merged.


Forcing Local Branch Deletion

If Git refuses to delete the branch and you’re sure it’s no longer needed:

git branch -D my-branch

This forces deletion regardless of merge status.

Use this carefully—unmerged work will be lost.


Deleting a Remote Branch

To delete a branch from the remote (commonly origin):

git push origin --delete my-branch

This removes the branch from the remote repository.

After this, anyone still referencing the branch locally will see it as gone.


The Older Push Syntax (Still Common)

You may also see:

git push origin :my-branch

This works, but the --delete form is clearer and preferred.


Cleaning Up Remote-Tracking Branches

After deleting a remote branch, your local repo may still show it.

Prune stale remote-tracking branches:

git fetch --prune

Or explicitly for a remote:

git remote prune origin

This keeps your branch list accurate.


Listing Branches

List local branches:

git branch

List remote branches:

git branch -r

List both:

git branch -a

Always confirm what you’re deleting.


Common Workflow Pattern

A typical cleanup sequence:

git checkout main
git pull
git branch -d my-branch
git push origin --delete my-branch
git fetch --prune

This ensures:

  • you’re not on the branch being deleted
  • local and remote state stay in sync

Common Mistakes

  • Trying to delete the branch you’re currently on
  • Forgetting to prune remote-tracking branches
  • Forcing deletion without checking merge status
  • Deleting shared branches accidentally

A quick check avoids irreversible mistakes.


Practical Tips

  • Delete branches after merge, not weeks later
  • Prefer -d over -D
  • Prune remotes regularly
  • Avoid deleting protected branches
  • Coordinate deletions on shared repos

Branch hygiene improves team velocity.


Takeaways

  • Local and remote branches are deleted separately
  • -d is safe; -D is forceful
  • git push origin --delete removes remote branches
  • Pruning keeps local state clean
  • Regular cleanup prevents confusion

Deleting branches is simple—but doing it deliberately keeps repositories healthy.