Git & GitHub Interview Question #148
How would you clean up a messy feature branch before opening a pull request?
Security, Team Workflows, Troubleshooting & Senior Git/GitHub Scenarios Senior Advanced
Quick Interview Answer
First synchronize your knowledge of the target branch and inspect the feature branch's commits and diff. Remove accidental files, split unrelated work if necessary, and ensure the branch builds and tests successfully.
Detailed Explanation
First synchronize your knowledge of the target branch and inspect the feature branch's commits and diff. Remove accidental files, split unrelated work if necessary, and ensure the branch builds and tests successfully.
If the branch has not been shared in a way that makes rewriting unsafe, interactive rebase can reword, reorder, squash, fix up, or drop local commits. Then update the branch on GitHub using the team's approved method, commonly --force-with-lease after an intentional rebase. Do not rewrite commits other developers depend on without coordination.
Code Example
git fetch origin
git switch feature/interview-ui
git log --oneline origin/main..HEAD
git diff origin/main...HEAD
# If safe to rewrite this feature branch:
git rebase -i origin/main
# Test, then update the remote branch carefully:
git push --force-with-lease