Git & GitHub Course
Git & GitHub
/
Advanced

Squash Commits

Definition

The process of combining multiple sequential commits into a single, cohesive commit to keep the repository history clean and meaningful.

Explain Like I'm New

Nobody wants to look at a history filled with 50 commits named 'wip', 'fixed typo', 'trying again', 'please work'. Squashing takes those 50 tiny, messy commits and crushes them together into a single, beautiful commit named 'Added Shopping Cart Feature'.

Real World Example

GitHub has a massive green button called 'Squash and Merge' on Pull Requests. It automatically crushes your messy feature branch into one clean commit before merging it into `main`.

Common Use Cases

  • •Maintaining a clean, readable git log

Terminal Output

bash / terminal
// While you can squash locally using 'git rebase -i HEAD~5', // the most common way developers squash today is using the GitHub UI. /* When completing a Pull Request on GitHub, instead of clicking 'Create Merge Commit', click the dropdown arrow and select 'Squash and merge'. GitHub will take your 15 messy commits, bundle them into 1 commit, and place that 1 commit onto the main branch. */

Interview Questions

basic

  • What command is manually used to squash commits locally?

intermediate

  • Why do Open Source projects demand that you squash your commits before they accept your code?

Flash Cards

Question

What command?

Click to reveal answer
Answer

Interactive Rebase (`git rebase -i`).

Question

Why Open Source demands it?

Click to reveal answer
Answer

Maintainers need to be able to easily read the project history. If a feature breaks 6 months later, they want to revert one single clean commit. They don't want to figure out which of your 40 'wip' commits caused the bug.