By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
A Hyper-Practical Guide for Engineers
You’re working on a feature branch with 10 small commits—typo fixes, debug logs, and minor refactors. When you merge into main, do you: - Keep all 10 commits (cluttering history)? - Squash them into one clean commit (losing granularity)? - Rebase interactively to rewrite history (risking conflicts)?
main
Merge strategies determine how your work integrates into the main codebase. Choose wrong, and you’ll: - Pollute git log with noise, making debugging harder.- Break bisect (Git’s binary search for bugs) if commits aren’t atomic.- Cause merge conflicts that waste hours to resolve.- Violate team conventions, forcing rework before PR approval.
git log
Real-world scenario:You’re a DevOps engineer maintaining a microservice. A teammate opens a PR with 20 "WIP" commits. The CI pipeline fails on the 15th commit, but the PR is approved. If you merge as-is, main now contains broken code. Squash merging would collapse all commits into one, ensuring main only gets working code.
git rebase -i
git --version
Goal: Collapse 5 commits into 1 before merging to main.
Create a feature branch and make commits: bash git checkout -b feature/login echo "Add login button" > login.html git add login.html git commit -m "Add login button" echo "Fix button color" >> login.html git commit -am "Fix button color" echo "Add debug log" >> login.html git commit -am "Add debug log" echo "Remove debug log" >> login.html git commit -am "Remove debug log" echo "Update docs" >> README.md git commit -am "Update docs" git push origin feature/login
bash git checkout -b feature/login echo "Add login button" > login.html git add login.html git commit -m "Add login button" echo "Fix button color" >> login.html git commit -am "Fix button color" echo "Add debug log" >> login.html git commit -am "Add debug log" echo "Remove debug log" >> login.html git commit -am "Remove debug log" echo "Update docs" >> README.md git commit -am "Update docs" git push origin feature/login
Open a PR on GitHub:
feature/login
Click Create Pull Request.
Squash merge via GitHub UI:
Click Squash and merge.
Verify: bash git checkout main git pull git log --oneline -5 Expected output: abc1234 (HEAD -> main) Add login feature def5678 Previous main commit
bash git checkout main git pull git log --oneline -5
abc1234 (HEAD -> main) Add login feature def5678 Previous main commit
Goal: Rebase a feature branch onto main before merging.
bash git checkout main git pull git checkout feature/login git rebase main
If conflicts occur, resolve them, then: bash git add . git rebase --continue
bash git add . git rebase --continue
Fast-forward merge: bash git checkout main git merge feature/login # Fast-forwards if no conflicts
bash git checkout main git merge feature/login # Fast-forwards if no conflicts
Verify: bash git log --oneline --graph Expected output: Linear history with no merge commits.
bash git log --oneline --graph
Goal: Squash "Fix typo" commits before pushing.
Start interactive rebase: bash git rebase -i HEAD~4 # Edit last 4 commits
bash git rebase -i HEAD~4 # Edit last 4 commits
Edit the rebase todo list: pick abc1234 Add login button squash def5678 Fix button color squash ghi7890 Add debug log squash jkl0123 Remove debug log pick mno4567 Update docs
pick abc1234 Add login button squash def5678 Fix button color squash ghi7890 Add debug log squash jkl0123 Remove debug log pick mno4567 Update docs
Save and exit.
Edit the new commit message: ``` Add login feature with button
Includes button color fix
Removes debug logs ```
Push changes (force push if already pushed): bash git push origin feature/login --force
bash git push origin feature/login --force
develop
feat:
fix:
docs:
git rerere
bash git config --global rerere.enabled true
non-fast-forward
git pull --rebase
git pull
Answer: All commits are combined into one. History is cleaner but loses granularity.
Scenario: "You rebase a branch and push. Teammates complain about conflicts. Why?"
Answer: Rebasing rewrites commit hashes. Teammates must git pull --rebase to sync.
Trick Question: "Which merge strategy preserves all commit history?"
Challenge:You have a branch feature/payment with 3 commits: 1. Add payment form 2. Fix typo in form 3. Add validation
feature/payment
Add payment form
Fix typo in form
Add validation
Squash the last two commits into the first one using interactive rebase.
Solution:
git rebase -i HEAD~3 # In the editor, mark commits 2 and 3 as "squash" git push origin feature/payment --force
Why it works: Interactive rebase lets you edit commit history before pushing.
git merge --no-ff
git merge --no-ff feature/login
git rebase main
git checkout feature/login && git rebase main
git rebase -i HEAD~3
squash
git merge --squash feature/login
git merge --squash feature/login && git commit
git push --force-with-lease
git push origin feature/login --force-with-lease
⚠️ Never git push --force to main or shared branches!
git push --force
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.