Bird
Raised Fist0
Gitdevops~10 mins

Squashing commits in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Squashing commits
Start: Multiple commits
Run git rebase -i HEAD~N
Editor opens with commit list
Mark commits as 'squash' or 'fixup'
Save and close editor
Git combines commits
Edit combined commit message
Save and close editor
Rebase completes with squashed commit
Result: Single combined commit
Squashing combines multiple commits into one by interactive rebasing and editing commit messages.
Execution Sample
Git
git rebase -i HEAD~3
# In editor, mark second and third commits as 'squash'
# Save and close editor
# Edit combined commit message
# Save and close editor
This sequence squashes the last 3 commits into one commit with a combined message.
Process Table
StepActionGit StateUser InputResult
1Run 'git rebase -i HEAD~3'Last 3 commits separateNoneEditor opens with 3 commits listed
2Mark commits to squashEditor shows commitsMark 2nd and 3rd commits as 'squash'Commits marked for squashing
3Save and close editorGit starts rebasingNoneGit combines the commits into 1
4Edit combined commit messageRebase paused for message editWrite combined commit messageCommit message updated
5Save and close editorRebase continuesNoneRebase completes with 1 squashed commit
6Check git logOne combined commit replaces 3NoneHistory shows single squashed commit
💡 Rebase completes successfully with commits squashed into one.
Status Tracker
VariableStartAfter Step 3After Step 5Final
Commit count31 (after squash)11
Commit messages"A", "B", "C""A + B + C combined""A + B + C combined""A + B + C combined"
Key Moments - 3 Insights
Why do we need to mark commits as 'squash' in the editor?
Marking commits as 'squash' tells Git which commits to combine. Without this, Git won't know which commits to merge. See execution_table step 2.
What happens if you don't edit the combined commit message?
Git will use the default combined message from all commits. Editing lets you write a clear, single message. See execution_table step 4.
Why does the rebase pause after combining commits?
It pauses to let you edit the new commit message before finishing. This ensures the final commit message is meaningful. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Git combine the commits?
AStep 3
BStep 2
CStep 5
DStep 1
💡 Hint
Check the 'Result' column in step 3 where commits are combined.
According to the variable tracker, how many commits remain after step 5?
A3
B1
C2
D0
💡 Hint
Look at 'Commit count' after step 5 in variable_tracker.
If you forget to mark commits as 'squash' in the editor, what happens?
AGit will combine all commits anyway
BGit will abort the rebase
CGit will leave commits separate
DGit will delete commits
💡 Hint
Refer to execution_table step 2 where marking commits is required.
Concept Snapshot
git rebase -i HEAD~N
# Opens editor with last N commits
# Mark commits as 'squash' or 'fixup' to combine
# Save and close editor to start rebase
# Edit combined commit message
# Save and close to finish
Result: Multiple commits become one with combined message
Full Transcript
Squashing commits means combining several commits into one. You start by running 'git rebase -i HEAD~N' where N is how many commits back you want to combine. Git opens an editor listing those commits. You mark the commits you want to squash by changing 'pick' to 'squash' or 'fixup'. After saving and closing the editor, Git combines those commits into one. It then pauses so you can edit the new combined commit message. After saving that, the rebase finishes. The commit history now shows one single commit instead of many. This helps keep history clean and easier to understand.

Practice

(1/5)
1. What is the main purpose of squashing commits in Git?
easy
A. To revert the last commit without changing history
B. To combine multiple commits into one for a cleaner history
C. To create a new branch from the current commit
D. To delete all commits from the repository

Solution

  1. Step 1: Understand commit history management

    Squashing is used to combine several commits into a single commit to simplify the commit history.
  2. Step 2: Identify the purpose of squashing

    This helps keep the project history clean and easier to read by reducing clutter from many small commits.
  3. Final Answer:

    To combine multiple commits into one for a cleaner history -> Option B
  4. Quick Check:

    Squashing = combine commits [OK]
Hint: Squash = combine commits to clean history [OK]
Common Mistakes:
  • Thinking squashing deletes commits permanently
  • Confusing squashing with branching
  • Believing squashing reverts commits
2. Which Git command starts an interactive rebase to squash commits?
easy
A. git commit --squash HEAD~3
B. git merge -i HEAD~3
C. git rebase -i HEAD~3
D. git reset --soft HEAD~3

Solution

  1. Step 1: Identify the command for interactive rebase

    The command to start an interactive rebase is git rebase -i followed by the commit range.
  2. Step 2: Confirm the correct syntax

    git rebase -i HEAD~3 opens the last 3 commits for editing, allowing squashing.
  3. Final Answer:

    git rebase -i HEAD~3 -> Option C
  4. Quick Check:

    Interactive rebase = git rebase -i [OK]
Hint: Use git rebase -i to start squashing commits [OK]
Common Mistakes:
  • Using git merge -i which does not exist
  • Trying git commit --squash which is invalid
  • Confusing reset with rebase for squashing
3. Given these commits:
commit1: Add README
commit2: Fix typo
commit3: Update README formatting
If you run git rebase -i HEAD~3 and squash commit2 and commit3 into commit1, what will the commit history show?
medium
A. One commit combining messages from commit1, commit2, and commit3
B. Three separate commits unchanged
C. One commit with message from commit1 only
D. Two commits: commit1 and combined commit2+commit3

Solution

  1. Step 1: Understand squash behavior in interactive rebase

    Squashing merges commits into one, combining their changes and commit messages.
  2. Step 2: Result of squashing commit2 and commit3 into commit1

    The final commit will include all changes and combined commit messages from all three commits.
  3. Final Answer:

    One commit combining messages from commit1, commit2, and commit3 -> Option A
  4. Quick Check:

    Squash merges commits and messages [OK]
Hint: Squash merges commits and their messages together [OK]
Common Mistakes:
  • Assuming only the first commit message remains
  • Expecting commits to stay separate after squash
  • Thinking squash deletes commit messages
4. You ran git rebase -i HEAD~4 to squash commits but got a conflict error. What should you do next?
medium
A. Manually fix the conflicts, then run git rebase --continue
B. Abort the rebase with git rebase --abort and try again
C. Run git reset --hard to discard all changes
D. Push your changes immediately to remote to fix conflicts

Solution

  1. Step 1: Understand conflict during rebase

    A conflict means Git needs you to fix code differences manually before continuing.
  2. Step 2: Resolve conflicts and continue rebase

    Fix the conflicts in files, then run git rebase --continue to proceed with squashing.
  3. Final Answer:

    Manually fix the conflicts, then run git rebase --continue -> Option A
  4. Quick Check:

    Fix conflicts + git rebase --continue [OK]
Hint: Fix conflicts manually then git rebase --continue [OK]
Common Mistakes:
  • Aborting rebase without trying to fix conflicts
  • Using git reset --hard which discards work
  • Pushing incomplete changes to remote
5. You squashed commits locally and want to update the remote branch. What is the correct command to push your changes safely?
hard
A. git push --all origin
B. git push origin main
C. git push --no-verify origin main
D. git push --force-with-lease origin main

Solution

  1. Step 1: Understand effect of squashing on commit history

    Squashing rewrites commit history, so the remote branch history differs from local.
  2. Step 2: Use force push safely

    To update remote with rewritten history, use git push --force-with-lease to avoid overwriting others' work accidentally.
  3. Final Answer:

    git push --force-with-lease origin main -> Option D
  4. Quick Check:

    Force push safely after squash [OK]
Hint: Use git push --force-with-lease after squash [OK]
Common Mistakes:
  • Using normal git push causing rejection
  • Using --no-verify which skips hooks but not force push
  • Pushing all branches unnecessarily