Useful Git Workflows

Squash Commits

Sometimes you have made multiple commits as you go, but in reality the work would have made more sense as one commit. This is probably most common when you are fixing spelling errors, or backing out code that didn’t end up working out after all.

Here’s a basic example of how to squash multiple commits together into a single one

git rebase -i HEAD~3

The -i flag is for interactively rebasing. This is what is going to let you pick what commits to squash, and what to squash those commits into.

The HEAD~3 represents the ‘to’ and ‘from’ range of commits you want to see, so that you can pick how to squash them. Here we use HEAD to signify starting at the most recent commit, and 3 is how many commits back to include.

this will open up a screen like…

pick asd89adf a commit message
pick 3ad8f98f another commit message
pick 12e8f9a2 bet you can't guess what this is

which you will change to…

pick   asd89adf a commit message
squash 3ad8f98f another commit message
squash 12e8f9a2 bet you can't guess what this is

this will take the second and third commits, and merge them into the first commit above them, as if it was always part of that original commit