Squashing Commits Video Tutorial


In this video you will learn how to, and why you may want or need to squash your git commits.

Simply put, squashing commits is the way of combining multiple commits into one commit.

Why might you want to do this?

The main reason is that one large commit is easier to process for the person reviewing your commits.

Another reason is that as you work on the bug, feature, or change on your local machine, you may well commit many small changes over time by way of giving yourself 'save points'. Particularly on larger projects / open source projects, the project commit logs can get very large and these small commits can create a lot of noise.

Instead, we can take two or more of our commits and squash them together into one single consolidated commit. We can give this new commit a more accurate / descriptive message to describe the purpose of the entire change as opposed to having to read all the individual messages and try to piece them together to form a cohesive view of the change.

Warning

Do not follow through with this procedure if you have already pushed your changes to a remote repository.

That's not to say you cannot use this technique moving forwards, but if - as an example - you have pushed up five commits to Github or Bitbucket or wherever, do not then squash these commits and re-push. This will cause all kinds of chaos.

How To

If you want to follow along, and you would like the .gitignore file as shown in the video, you can find that here.

Secondly, if you're wondering how to use the short hand for git, check out ohmyzsh.

The commands you will see in this video are:

git init

That one should be pretty self explanatory :)

ga . 

This is shorthand for adding all your new and modified files (excluding deleted files) to the git staging area.

gc -am "message"

This is shorthand for `git commit -a -m "message".

gst

This is shorthand for git status

git log

For seeing the current git log status.

git rebase -i HEAD~3

This is where the magic happens :)

In this command we are starting our rebase in interactive mode (-i). We are saying go back 3 commits (~3) from the HEAD (the current position).

From here it's a case of choosing which commits will be included in our squash, saving the selection, and then letting Git do its thing. After that, we are good to push.

You can see the end result in the git repository we created in the video by clicking here.

Further Reading

Code For This Course

Get the code for this course.

Episodes