Have you ever wanted to combine multiple git commits into 1? I know I have! I’m a bit embarrassed to push a piece of code to github and have 10 commit messages while I only edited 1 file. Since I work on 3 different work stations, I constantly push code to git and continue working on it from my other work station. You can imagine how ugly the commit history looks by the end of the day. Here’s an example:
To avoid this problem, I used to make a backup of my latest code, delete all commits and push my new code. If I only knew about squash earlier (no, not the fruit).
Why don’t we “squash” these commits into 1?
Looking at my logs (git log
) I see that I made 16 commits (Jeez).
Lets squash these ugly commits! Run the following command:
git rebase -i HEAD~16
This will show all 16 previous commits on my current branch (master).
Lets pick the first commit (SHA: 59b6bfa
) and squash the rest (change pick
to squash
).
Save and quit (Since I use vi, type :wq
to save and close the current file).
Git will now give us a second change of being able to change the commit messages (incase you didn’t like them before).
In this case, I was happy with the previous commit messages and I didn’t want to change them.
Save and quit (Again, with vi is :wq
) and you should see that the commits have been combined.
Bam! Now lets do a git push
and see if those ugly commits was changed to 1 commit:
Yes! This is probably one of the best feature in github 🙂