Anas Anjaria
Anas Anjaria's blog

Anas Anjaria's blog

Rewrite Commit History With Git Interactive Rebase

Photo by Yancy Min on Unsplash

Rewrite Commit History With Git Interactive Rebase

And keep your commits clean.

Anas Anjaria's photo
Anas Anjaria
·Aug 31, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Whenever we work on something, we review our work after completion before presenting it to our audience. Similarly, our code needs reviews by our colleagues before going into the production system.

While adjusting requested changes by our colleagues, it’s crucial to keep our commit history clean.

As a beginner, I used to add a new commit to adjust the requested change. But it makes commit history ugly.

With git interactive rebase, you can rewrite commit history and keep your commits clean.

I have shared 3 different use cases using git interactive rebase in this post that you might find helpful.

Adjusting requested changes by colleagues

Our coworkers review our code before it goes into the production system. When the team requests changes, we can adjust the requested changes directly by editing a specific commit. For instance, our colleague requests changes in the commit message Optimize bulk endpoint.

We can edit that commit using git rebase -i main. An editor window will open that lists all the commits. We can choose what action to perform on which commit. As we like to edit the second commit, we will select e or edit as shown below and save it.

git-interactive-rebase.png

Now, we are editing this specific commit. Once we adjust all the requested changes, we can commit our changes by running the following commands.

git add .
git rebase --continue

Merge multiple commits into one

Interactive rebase also allows us to merge multiple commits into one by using the squash command.

How can this be useful for us?

I use this feature in the following scenarios.

  1. Sometimes I am unsure how to organize my commits, so I add temporary commits. As I progress with my task, it makes the picture clearer. Once it’s clear, I merge those temporary commits into one meaningful commit.
  2. Sometimes I would like to try something new. Before experimenting with new stuff, I add a temporary commit. If my experiment is successful, I will merge it with my previous work or discard everything otherwise.

You can merge commits by using git rebase -i main and use s or squash against the commit you want to merge as shown below.

git-interactive-rebase-1.png

This will lead to the following window.

git-interactive-rebase-2.png

We need to select which commit message we want to keep and which one to ignore using # as shown below.

git-interactive-rebase-3.png

Upon saving, git merges the 2 commits into one.

Dropping a commit

We can drop a specific commit by using d or drop command against a specific commit as shown below

git-interactive-rebase-4.png

I use this feature when it is unclear whether my coworkers will accept my work or not.

Ideally, I discuss it with my team before working on it. But if the colleagues are busy and the changes are not time-consuming, I make it a separate commit. This way it will make it easy to drop my work if required.

Thanks for reading.

Originally written on medium


Want to connect?

anasanjaria.bio.link

 
Share this