🔀 Git & GitHub Intermediate

What is squash merging and when should you use it?

Why Interviewers Ask This

Mid-level Git & GitHub roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

Squash merging combines all commits from a feature branch into a single commit when merging into the target branch. The feature branch's individual commits (including WIP, typo fixes, "oops" commits) are squashed into one clean commit on main. GitHub pull request squash option: "Squash and merge." Command line: git merge --squash feature-branch — this stages all changes without committing; you then create one commit: git commit -m "Add user authentication feature". The feature branch history is discarded — only the final squashed commit appears on main. Pros: (1) Clean main branch history — each commit represents one complete feature or fix; (2) Easy to revert a feature (one commit to revert); (3) git log on main is readable without feature branch noise. Cons: (1) Lose the detailed commit history of the feature (can't see the intermediate steps); (2) Makes individual commits harder to bisect; (3) The feature branch must be deleted after — it can't be "merged" again. When to squash: small-medium features with messy commit history; when team prefers a clean, readable main branch history. When NOT to squash: large features where individual commits have meaning; when you want to cherry-pick specific commits later.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Git & GitHub experience.