How to squash in Git?
There are two ways that I know of to make squash, in this post I will show you both ways. But first I will tell you the definition of Squash
What is Squash? #
Git squash, (“compress”), makes our branch to merge (to which we will make a pull request) be composed of a single commit in this way we will merge a single code commit to our development branch that will contain a functionality complete.
Now yes, to the steps #
Standing on the branch we want to squash, now on the console we'll do
git log
will return
Something like that, from there we are going to copy the hash that is above origin/main, main
in my case it is this b6adbbcd8f99935113cf1e24943736fb2c501e26
Then we're going to rebase
git rebase -i TU-HASH
in my case it is
git rebase -i b6adbbcd8f99935113cf1e24943736fb2c501e26
It will show you something like this, it will open the VIM editor
You will have to replace all the lines that say pick
with squash
or s
except the first line
To write in VIM you must hit the i key
it will stay like this
Then we will press the Esc
key twice
when doing that, you will have to type :wq
and hit enter
There it will ask you for the final message for the compressed commit
You place the message, pressing i
to type, then pressing Esc
twice and typing :wq
And that's it, you already did Squash
Now you must to upload it to your repository, just write
git push -f
And ready, I'd be ready
The other way to do Squash #
To do this, standing on the branch to make squash, he places
git reset --soft TU-HASH
Previously I explained how the hash is obtained
then you will
git commit --amend
The VIM editor will open, to put the final message of the commit, press i
to type
Then you press Esc
and type :wq
to save and exit
Now with
git push -f
Your changes will be uploaded to your repository
If you do git log
now, you should have 1 single commit above origin/main
with the name you gave him in ammend
Do not do git reset --hard unless you want to erase all your progress
Additional features #
I recommend using Conventional Commits for your commit messages