Deleting Branches

17.1 Deleting a Local Branch

After a branch has been merged:
1git branch -d branch-name

Example:
1git branch -d feature/login-auth

Git performs a safety check and refuses if the branch is not fully merged.

17.2 Force Deleting a Local Branch

If the branch is not merged but you still want to delete it:
1git branch -D branch-name

This skips safety checks.
⚠️ Use carefully — unmerged commits may be lost.

17.3 Deleting a Remote Branch

To delete a branch from a remote (like GitHub):
1git push origin --delete branch-name

Example:
1git push origin --delete feature/old-ui

This removes the branch from the remote repository.

17.4 Deleting Remote-Tracking References (Cleanup)

Sometimes remote branches are deleted but still appear locally.

Clean them up using:
1git fetch --prune
or
1git remote prune origin

This removes stale remote-tracking branches.

17.5 Best Practices

Delete feature branches after merging
Never delete shared branches like main or develop
Clean up stale branches regularly
Coordinate before deleting someone else's branch

17.6 Key Takeaways

Use git branch -d for safe local deletion.
Use git branch -D to force delete unmerged branches.
Remote branches are deleted with git push origin --delete.
git fetch --prune cleans stale remote references.
Regular cleanup keeps repositories readable and manageable.