When you are working with version control. You may have multiple commits that you would like to combine into a single commit. Or perhaps you want to change their order, etc…
First you need to determine how far back you want to go. Open a command prompt or terminal window, navigate to your Git Repository.
git log -10
The above command will show the last 10 commits. If that does not go far enough back, in increase the number 10
Once you have identified the commits you wish to squash run the git rebase command:
git rebase -i HEAD~10
The above command will open a git rebase window showing you the last 10 commits. Adjust this number as needed.
Each line shown will have the word pick here is an example of the rebase window:
Any commits you wish to squash, replace the word “pick” to the letter “s”
Be sure to leave the last commit in the list set to pick and the first commit in the list set to pick. As you squash the commits, they will be combined into the previous commits. You need to leave something for it to combine into. If you don’t do this, you may get an error.
In our example, we can really only squash the middle commit since we have 3. If you want to squash that first commit, add another commit before squashing.