The git merge approach is as follows:
git checkout <branch_name_into_which_you_want_to_merge>
git merge --squash <branch_name_to_be_squashed>At this point, you might have to fix some conflicts. Do so.
Use git commit if you want to edit a pre-formatted message with all squashed commit messages.
Or use git commit -m “<your_commit_message”> if you want to override the pre-formatted message.
git push --force-with-lease
The interactive rebase approach goes like this:
git checkout <branch_name_to_be_squashed>
Check your Git tree, identify the first commit of the branch, and save its sha512 id.
Or count from there to the last one of the branch and save the number of commits there are, including the first one.
If you went with the sha512 id: git rebase -i <sha512_id>.
If you went with the count: git rebase -i HEAD~<count> (respect the spaces, as these are important).
Your configured text editor will open with the action to take to each sha512 id commit and its respective message.
All commits are organized from older to newer, top-down. There’s also a commented message (# in the beginning of the line)
on how to proceed for each commit.
You can do a lot of stuff with them, but we’ll focus on the squash action.
Keep in mind that you need at least one commit to be picked before the one you want to squash in order to be able to do so,
which means you can’t choose to squash the first one.
Every commit you squash will be squashed into the previous one that was executed.
So, for example, if you squash the second one, it goes into the first one.
Pick your actions for each commit.
I strongly advise against drops and deleting lines,
which will discard that commit (unless you’re really sure on what you want to do),
and changing the order of the commits, since they will be reapplied in a different position than they were created.
Save the file and quit the text editor.
A new text editor will appear. In this one, you’ll need to write the messages for the new commits.
This text editor will appear for each new commit you have and will show the messages of all squashed commits into that one
and the message for that one. You can either choose one of those or write one of your own.
Again, save the file and quit the text editor.
At this point, you might get a message saying the rebase was successful.
If so, the only thing left is to run git push --force-with-lease and the squash is finished.