The 'git rebase -i' Command

Learn about the “git rebase -i” command and how it can help you to squash commits.

In order to squash a set of commits, you need:

  • A reference to the last (latest) commit of the set you want to squash.
  • A reference to the oldest (first) commit of the set you want to squash.

You already have a stable reference to the latest commit, i.e., where we are at the moment. Can you remember what it is?

You need a reference to the oldest commit in the history. Can you find a way to get it?

Reference to oldest commit

There are many ways to do it. The way we’re going to find it is to use the git rev-list command. You should be at the point where you’re comfortable reading up on a Git command, so go off and do that now.

OK? Now, run this command:

1	git rev-list --max-parents=0 HEAD

That gets you the original commit, and that’s the method you’re going to use here.

The following command will substitute the output of that command into the git rebase command below:

2	git rebase -i $(git rev-list --max-parents=0 HEAD) HEAD

Get hands-on with 1400+ tech skills courses.