Squashing commits means merging multiple small commits that are all related into one big commit to contain all the features of the commits you squashed. This is used to clean up the hstory of your repository so there's no unnecessary commits that clutter up your history thus making it easier to browse through.
- For example, you just finished working on your code and you commited it but then you realized you had to change one of the variables and then has to commit that seperately. In this case you can squash it into the previous commit and have only one commit to represent both of those changes.
There is no git squash
command, to squash you must use merge
or interactive rebase
.
Note: You do not need to complete this by yourself but must know the commands for the practical as you will be asked to squash commits.
-
To squash multiple commits using interactive rebase, in your terminal type:
git rebase -i HEAD~n
Note: The
-i
is used to enter interactive rebase andHEAD~n
includes n commits from HEAD in the rebase. -
A window should open with all the commits you selected. Change the first word from
pick
tosquash
for each of the commits except for the one on top(this is the commit where all the others will be merged to). -
After closing the previous window another one will prompt you to enter the commit message, do so and close the editor window.
-
Finally you can push this commit using
git push