Getting Git

A comprehensive video course from git init to Git Master.

Master: git rebase


In this video we'll learn some of the additional usages of git rebase.

In init: git rebase we learned how to use git rebase to bring a stale branch up-to-date.

We can also use git rebase to interactively change the commit history. This can be useful when you're making frequent commits while working on a branch that you might want to clean up before merging.

Let's demonstrate this by making a few changes and commits so we can interactively rebase them.

I'll commit a new file, then change it by adding a timestamp and commit that change.

I'll add another timestamp to the File 1 and commit that change.

Finally, I'll commit another new file, add a timestamp and commit that change.

If I run git status to verify I'm in a clean state and run git log -5 to see the commits I just made.

Now I'm at a point where I've completed my work. But looking at the commit history, I might want to clean up some of these commits.

I can do so by using the -i option. This stands for interactive and allows me to modify a set of commits.

Let's demonstrate this by running:

get rebase -i SHA

The SHA is the last commit you want to keep. This commit is not included in the rebase.

In this case, I'll pass the SHA of the first commit followed by the caret to reference the commit before.

Without the caret, the first commit would not be included in the rebase.

When we run this command Git opens a text editor and lists the commits similar to how they appear when we run git log --online.

There's a few differences.

First, they commits are listed in chronological order. So the oldest commit is first.

Second, each line is prefixed with a command. The default is pick. There's a legend in the comments explaining the other commands.

Let's go through these one by one.

  • pick means keep the commit.
  • reword allows me to edit the commit message.
  • edit will pause the rebase process so you can make more changes to the commit.
  • squash will condense the commit into the previous commit and let you edit the commit message.
  • fixup will condense the commit into the previous commit without editing the commit message.
  • drop will remove the commit or you can simply delete the line

So, let's condense our first two commits by making the second command squash.

We'll remove our third commit by deleting the line.

And we'll use fixup on our last commit to condense it with the previous commit, but this time we won't edit the commit message.

When we save and close, the rebase process starts modifying our commits with the commands we provided.

It stops on the second of five commits to allow use to edit the commit message.

I'm going to change the message to just Adding file 3 with a timestamp.

When I save and close, the rebase process continues and completes the remaining commands.

If I run git log -5 again, we see we turned our 5 commits to 2, by condensing the second and last commit and dropping the third commit.

Now that we have a cleaner commit history we're ready to merge into master.