Working with Git and Visual Studio – Use Git Rebase inside Visual Studio

This blog post is part of in-depth blog series on the working with Git command line and Git in Visual Studio. You can find the previous blog post here. In previous blog post, we discussed how to use git rebase commands, the effects of same on the branching strategy and also rebasing on the same branch. In this post, we’ll discuss how to use git rebase inside Visual Studio.

Re-creating Problem Scenario

For starters, we have two branches named master and newQuickFix branch in our repository on to which we have made some commits. We’d made different commits in the two branches so that their commit history is different.

Below is commit history for newQuickFix branch:

Commit history for newquick branch before rebase
Commit history for newquick branch before rebase

And for master branch:

Commit history for master branch before rebase
Commit history for master branch before rebase

We can see that both branches diverged after common commit 5bec225e.

Perform Git Rebase

Now we would like to incorporate code changes from master branch to newQuickFix branch. For this, we’ll need to checkout newQuickFix branch, right click and select ‘Rebase Onto…’:

Select Rebase Onto option
Select Rebase Onto option

In the list of branches available in the dropdown, select master branch and then select ‘Rebase’:

Select master branch in Onto branch options
Select master branch in Onto branch options

Visual Studio will now start process of Rebase using git. If we now see git commit history for newQuickFix branch, it would be something like this:

Commit history for newquick branch after rebase
Commit history for newquick branch after rebase

We can see that it is single linear tree now. Also commit SHA hash has changed for commits made in the newQuickFix branch even though commit message is same.

Perform Rebase on the same branch

Let’s reset previous changes on the newQuick branch using git rebase commands. This time we’ll select newQuick branch as onto branch:

Rebase on the same branch
Rebase on the same branch

Note that the rebase option is greyed out. As of now, Visual Studio does not offer this functionality. For this we can fall back to git command line.

Aborting Git Rebase

Once you understand what rebase does and when to use it, it is not that difficult to handle. Rebase has many useful options such as –skip or –abort. When you get confused, just run ‘git rebase –abort’ which reset everything to a state before rebase.

One thought on “Working with Git and Visual Studio – Use Git Rebase inside Visual Studio

Leave a comment