DEV Community

Cover image for My Squash vs That Squash

Posted on

My Squash vs That Squash

Recently I moved up to higher stakes in my training to be a good junior developer. I was placed in a team that values conventions set and would comment and mark pull requests as critical (this meaning it has to be sorted immediately). One unwritten rule that is not found on our Wiki is squashing commits. Its funny though because the team lead mentioned that our commits should not pass 5 commits. A peer developer, JK, commented on my pull request and told me that I had violated the 3
commit limit. I therefore went to our old friend Google. After opening and closing several Stack Overflow tabs, scanning through the git help pages I was ready to try out something. I decide to play around with a project I had made before. I did not want to screw up in any way the project, branches and commits I had already.(Just being safe, Junior dev fears)

I changed directory to a bucketlist app I had and then did a git log to see the last few commits I had. I then typed the following

# Reset the current branch to the commit just before the last 2:
git reset --hard HEAD~2

# HEAD@{1} is where the branch was just before the previous command
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}

# Now commit those squashed changes. The commit message will be helpfully
# pre-populated with the commit messages of all the squashed commits:
git commit
Enter fullscreen mode Exit fullscreen mode

This worked well for me.

Another friend of mine, BT, who joined later, was tasked with configuring continuous deployment on our project, wanted to open a pull request. I was standing next to him as he explained the nightmare he had but managed to get things running. JK came from a meeting and sat next to BT and asked if he had any issues with making a pull request. BT mentioned he did not know how to squash. JK said its simple and opening his laptop JK typed the command below on this laptop. I decide to lay back as I was still new.

git reset --soft HEAD~3
Enter fullscreen mode Exit fullscreen mode

He then explained that the number represented the number of commits you want to squash. In the above case 3 represents the last 3 commits you want to squash.

I was curious and I engaged JK about the above command and he explained his reasons.

1) My question is which command is easier?
2) Or is this the wrong question to ask?
3) Are these commands dependent on the project

I liked the command JK used despite having the satisfaction of finding a different answer to the same problem.

What do you use when squashing commits?

Thanks to Mark LongAir's answer on Stack Overflow

Top comments (4)

maestromac profile image
Mac Siri

I normally would use git rebase --interactive HEAD~{x} and squash things from there. It easier for me and I make fewer mistakes with it.

l_giraudel profile image
Loïc Giraudel

I use interactive rebase too, I think it's the easiest to reorder commits, merge similar ones, etc.

I wrote an article about, among other things, interactive rebase:

hawicaesar profile image

I will try this out.

mafzst profile image
Nicolas Perraut