Git Squashing

What is Git Squashing?

Git squashing = combine multiple continuous commits into one

Git squash

Git squash

When to use Git Squashing?

Purpose of Git squashing: to keep the branch graph clean 💪

Scenarios:

Implementing features
Usually, we’ll commit multiple times before we reach a satisfactory result, such as some fixes and tests. When we’ve implemented the feature, those intermediate commits look redundant. So, in this case, we may want to squash our commits into one.
Merging branches
When we start working on a new feature, we’ll start a feature branch. Let’s say we’ve done our work with 20 commits in our feature branch.
So, when we merge the feature branch to the master branch, we want to do a squashing to combine the 20 commits into one. In this way, we keep the master branch clean.

How to Git Squashing?

We can perform Git squashing via interactive rebase:

git rebase -i HEAD~{X}

X is the number of commits to be squashed.

If X is large and not easy to count, we can find the hash of the commit that we want to rebase “onto” (by calling git log) and directly rebase on it:

git rebase -i {hash_onto}

Example

A nich and clear video tutorial:

and its detailed description: Combining Git commits with squash

Reference