Interactive Rebase

Following along from my previous blog post on rebasing, this post will cover interactive rebasing.

The first thing to know about interactive rebasing is that as far as the programmer is concerned it has nothing to do with rebasing. The principal purpose of Interactive Rebase is to clean up your commits before you push them to the server.

To see this at work, let’s create the world’s dumbest C# program. For this purpose I assume you have git installed along with Visual Studio 2019 and that you have an account on GitHub. If not, they are all free, so go get ’em and I’ll wait here.

All set? OK, let’s create a C# Console app called dumbApp on GitHub

Next, clone that repo to your local machine (these steps are covered in previous blog posts and in my book “Git for Programmers”

 git clone

Now that we have our repo, let’s create a VS solution in that folder

With that in place, immediate commit the app

git add dumbApp
git commit -m "initial state of the program"

Do not push the commit.

Now add one line to the program and commit it again.

git add dumbApp
git commit -m "Added second line"

Do this for a few more lines, committing after each line

I added some meaningless lines but tried to make the messages fairly clear. When I use my nifty log command here is what I get:

❯ git lg
* db178aa - (HEAD -> main) Added second line to run (3 seconds ago) 
* 97eac96 - Added new class - run
* cdbab3c - Updated program.cs (3 minutes ago) 
* 5d4e385 - Initial state of the project (8 minutes ago) 
* 3932b3c - (origin/main, origin/HEAD) Initial commit (21 minutes ago) 

I don’t want to check all this in. Some of the lines are really just work in progress. Interactive Rebase allows me to “squash” some of the commits together before I push them to the server.

To do this, I go to my trusty command line and enter

git rebase -i

You can of course do this in Visual Studio or just about any GUI interface to Git.

Do not forget the -i; it means interactive and changes this from a rebase command to an interactive rebase command. Git will tell you it is waiting for your editor, and if your configuration file is set up right, that will be Visual Studio Code; which incidentally has tremendous support for interactive rebase.

I’m going to squash a few of my commits in to the commit above it, thus reducing the number of commits another developer examining these commits (after a push) will have to slog through…

Pick indicates that you want to keep that commit. Squash indicates that you want to combine it with the one above. Thee are a few other options but these are the ones you’ll use from day to day. Once you close the editor it will reopen and give you a chance to fix up your messages:

As you can see, I’ve cleaned up the comments for the new, squashed commits. I’m now ready to push the two commits. (I started with four but squashed two so they are combined with the one above into a single commit. At this point you can enter:

git push

and push your two commits to the server. If you now examine the history of your commits you will see only the ones you “picked” and your modified messages.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials. Bookmark the permalink.