Q: When should you use `git rebase` instead of `git merge`, and what is the one rule you must never break?
Both integrate changes from one branch into another, but they produce very different histories.
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "Git is an immutable directed acyclic graph (DAG); knowing commands like git reflog means you never truly lose commits. The interviewer is testing: Workflow tradeoffs, understanding the danger of rewriting shared history.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
Initial Diagnostics & Root Cause Analysis
Both integrate changes from one branch into another, but they produce very different histories.
- Merge preserves the actual branching history (creates a merge commit). Good for
main/release branches where the history of *when things converged* matters. - Rebase replays your commits on top of the target branch, producing a linear history with no merge commits. Good for cleaning up your local feature branch *before* opening a PR — it makes review easier and
git logreadable.
Remediation & Permanent Safeguards
The one rule: never rebase commits that have been pushed and that other people are working on. Rebase rewrites commit hashes. If a teammate has pulled the old commits and you force-push rewritten ones, their next pull is a mess and they may re-introduce the old commits. Rebase your private branch all you want; never rebase shared branches like main or develop. Common workflow: rebase your feature branch onto latest main (git pull --rebase origin main), squash interactively (git rebase -i), force-push to your *own* feature branch, then merge the PR.
- Merge preserves the actual branching history (creates a merge commit). Good for main/release bran...
- Rebase replays your commits on top of the target branch, producing a linear history with no merge...
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring rebase instead merge
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring rebase instead merge Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring rebase instead merge
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring rebase instead merge Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.