We all like to keep our code looking as neat as possible, but sometimes you also need to keep track of those small changes you’ve been making. A good way to ABC (Always Be Committin’) is to work in a branch and hack your way through the problem and then to clean up before submitting a PR.
Cut a new branch
What we are about to do here can be destructive as well as confusing. With tasks
like that, it’s always nice to have a backup and so we are going to cut a new
branch to work with off of the branch that all of the nasty
more changes live
git checkout -b squashed_feature
Rebase from master
This will give us a branch that we can then safely rebase from master. This process will allow you to pick the commits you would like to squash and the ones you would like to keep. You can do this by running:
git rebase -i master
If you have merged master into your branch during your development process you will be unable to use this method. I normally will rebase my branch on master instead of merging in, but workflows may vary. Once you begin the rebase process, your git editor will open a file that looks like:
In this view the most recent commit will be at the top and the oldest at the bottom. To squash all of the commits into a single one, change all the ‘pick’s but one to ‘squash’. Note: ‘s’ will also work instead of ‘squash’
You want to squash these changes as if you were to remove the line itself, the actual commit will be removed from history:
At this screen there is no need to change the commit messages. After saving and quitting the editor, a new file will open to allow you to edit the commit message. In that file, just remove all of the messages and replace them with the one that you want.
You can now push your branch up and open a PR!
git push origin squashed_feature