This blog is one of the git cool feature “Squash”.
Note : Because this is an advance feature of git, I am expecting that you have basic knowledge of git like push, commit, checkout, branching etc.
When we shall hear Squash first time, we would get three questions in mind, first one is ‘What is git squashing?’, second ‘Why we need it?’ and third one is ‘How we squash the commits?’.
If someone said to me give first question’s answer in one line then my answer will be “Squash is merging of commits”. The answer of second question I am explaining by one use case.
Suppose you are working on new module(User Registration). You created a new branch suppose user_registration from the master. You worked on this module 2 three days and you commit approx 20 commit for this module. Now customer want these commit as single commit with message of “User Registration Module” in the master branch because he also want same registration functionality in his other repository(by cherry-pick). What is the solution ?
Solution is Squash all the commit into a single commit.
Next question was “How we squash the commits?”.
The steps of squashing are given below
- First get commit hash, from which you want to squash the commits, by
git log Command
- Copy the hash and run the command
123[code]git rebase -i ce862832ace5f332b6f31155137a478ddeb3b95c[/code]
where i stand for interactive mode
Suppose we want to squash the commit having same messages.
- Now update the text ‘pick’ to ‘s’ or ‘squash’. In the given figure in am merging “be11c41 and 2dac049” and “535b7e9 , 8f86810 and 21bcd9e”. Squashing is always done bottom to top I.e a commit will be meld into previous commit. So is the above example first 535b7e9 will merge into 8f86810 and then 8f86810 will merge into 21bcd9e.
Now press cont+x. Prompt will ask question to Y or N. Press Y.
You will got a screen for new message which will be for the merged commits.
Update the message. In the example I have updated it to “Commit for new mobile api.
press cont+x. It will again ask you for message for second squash(for be11c41 and 2dac049). For example second updated message “Squash : commit for updating the vyykn_vital api”.
- Thats it. You will got the message “Successfully rebased and updated refs/heads/branch_name”
1[code]git log [/code]
again and you will found that commit for same message will be squash into one commit.