How to Squash Commits in Git
Easy Guide to Squash Commit with Problem Solving
Squash commits could be one of many tricky problems in iOS Development (and another project too). In this article, I want to give you a simple tutorial on how to squash commit with problem-solving tips.
Step-by-step Squash
1. Checkout to branch that you want to squash.
git checkout <branch name>
//example: git checkout develop
2. Check how many commits that you have in that branch.
git log
3. Squash it.
git rebase -i HEAD~<number of commits>
//example: git rebase -i HEAD~2
4. Pick which commits that you want to squash. If you want to squash that commit, type squash before commit number. If you want to choose commit that squashed into, type pick before commit number. It should look like this.
5. Press esc, then type :wq and press enter.
6. Type :wq and press enter again.
7. Force push your changes.
git push -f
Problem-shooting
If you had merge conflict errors when squashing, don’t panic. Just type this.
git mergetool
It will open an external merge tool, you can fix errors easily in that tool. Just scroll down the merge tool and find errors there. There is a red line in the scroll indicator if you have an error in which line. After finish fixing your error, just press cmd+s and exit the merge tool. Then continue your squash.
git rebase --continue
If you have more than two commits to be squashed, sometimes you have to do this things more than once. Don’t panic because it’s normal.
Squash Tips
Last but not least, a little tip from me is, always squash after you commit. Because the more commits you to have to squash into one commit, the conflict rate will be increasing too. Let squash be one of your habits after commit.