Deleting Branches
17.1 Deleting a Local Branch
After a branch has been merged:
Example:
Git performs a safety check and refuses if the branch is not fully merged.
1git branch -d branch-nameExample:
1git branch -d feature/login-authGit 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:
This skips safety checks.
⚠️ Use carefully — unmerged commits may be lost.
1git branch -D branch-nameThis 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):
Example:
This removes the branch from the remote repository.
1git push origin --delete branch-nameExample:
1git push origin --delete feature/old-uiThis 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:or
This removes stale remote-tracking branches.
Clean them up using:
1git fetch --prune1git remote prune originThis 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
• 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.
• 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.