It happens to me very often that someone help me out on a branch and pushes their code on a fix up commit. Every time it happens, I panic because I want to squash this commit into my previous commit so the git history looks clean. I used to be very bad at git, my tree will have unexpected merges and all. However, I decided to learn and focus on this point this last year to avoid these silly mistakes and get better at it. Here is how I managed to squash my commits using the command line. Note that you might use a graphical git manager, this could make this process easier.
Start an interactive rebase
Squashing commits means that you have to start an interactive rebase. Choose the starting commit, for me itβs 2 because I just want to squash a fix up commit in my previous commit.
git rebase -i HEAD~2
At this point your editor of choice will pop up, This is the file that will open:
pick 571afa fixup commit
pick 652fec feature: commit message specific to the feature
Replace the pick by squash, or s for brevity, in front of the commit you want to squash. Then save the file and close it.
squash 571afa fixup commit
pick 652fec feature: commit message specific to the feature
A new window will open with the 2 messages of the commits you want to squash together, change the name of the commit if you want one clean commit message and save. Then run:
git push -f
You can check the outcome on your git repository, only one commit gathering the modifications you did on your two old commits should be in the git history. Easy right?!
Top comments (0)