Git & GitHub Interview Question #145
What should you do before deleting a local or remote branch?
Security, Team Workflows, Troubleshooting & Senior Git/GitHub Scenarios Senior Advanced
Quick Interview Answer
Confirm that the branch's required work has been merged, rebased, cherry-picked, tagged, or otherwise preserved. Check the branch commits relative to the target branch and verify that no active pull request or deployment still depends on it.
Detailed Explanation
Confirm that the branch's required work has been merged, rebased, cherry-picked, tagged, or otherwise preserved. Check the branch commits relative to the target branch and verify that no active pull request or deployment still depends on it.
For remote deletion, consider other collaborators who may still be using the branch. A branch name can be deleted while its commits remain reachable elsewhere, but unmerged unique commits may eventually become difficult to recover.
Code Example
git fetch --all --prune
# Show commits unique to the branch
git log main..feature/old-work --oneline
# Safe local deletion if merged
git branch -d feature/old-work