Git: Editing your commits with rebase - part 1/2

The more I work with git, more I realize how powerful it is. Sometime ago I discovered that there is a way to organize/edit your commits. For what? Some useful cases:

  • the commit message is wrong or it doesn’t make sense.
  • the order of the commits is not nice regarding to GitHub history.
  • there are more than one commit which make similar changes (or even the same thing).
  • a commit grouped a lot of different code and it makes sense divide it in smaller commits.

A quick example

A simple example: let’s imagine that in your project you did a change and made a commit:

So, you just remembered that you needed to update the README.

So, you went to get a cup of coffee and when you were returning to your desk, you remembered a last CSS adjustment that you have to do. So you do it, and make a new commit:

A simple log to see the commits would show something like that (the last 3):

If it’s a small or even personal project, we could say that is okay to let the commits this way. However, if you are working with other people in a big project, it could be weird three commits for the same and small change. For help us, there is the interactive rebase: using it we could change the commits in a branch.

How I do it?

About the code above:

  • -i => interactive mode
  • -3 =>number of commits we want to target.

Running that comand, a screen like this below will show (it will open in your default editor like Vim):

Reordering commits

In example above we could change the commits’ order.

And.. it’s done! If everything is ok, a message like that will appear:

Some conflicts could happen, and in this case the rebase command will stop untill you resolve the conflicts. After that you only need to run git rebase --continue to continue or git rebase --abort to quite and abort the process.

Editing messages

Other thing really cool is the possibility to edit the message of the commit. In the previous examples, now we want to change message of the commit which updates the README file.

So, we run again the rebase.

And the same screen with the commits list will show for us. Now we change the pick word to reword in the commit which we want to change the message.

Doing that, a new screen will show for us:

We just need to type the new message.

And it’s done! If we run a simple log, we will see the commits list with the message updated:

Forcing the push

As reminded by Cicero Pablo, when we use the interactive rebase, if you already have a repository with a history of commits, you will have to use the push command with the --force flag.

There is more…

You can read the second part of this article that we talk about merge and split commits.

Some points:

  • The names/structure of the files and message of commits are only for example.
  • I used the word screen to make reference to every return of terminal after a command.
See all posts...