Working with Git and Visual Studio – Use git rebase with git command line

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 what is git rebase, how it is different from git merge and when to use the rebase command. In this blog post, we’ll follow that up by using git rebase commands at git command line and understand it further.

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. Below is the git commit history which shows that both branches are pointing to same commit:

Git commit history details - 01
Git commit history details – 01

Let’s switch to branch newQuickFix and add couple of files, make couple of commits. So that our history of new QuickFix branch looks like this:

Commit history for newQuickFix branch before rebase
Commit history for newQuickFix branch before rebase

We’ll also go to master branch, modify a few files and commit the same. So that both branch will diverge. Now, our master branch has below commit history:

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

If we now observe the git history for all branches, it would be like this:

Commit history for all branch before rebase.PNG
Commit history for all branch before rebase

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 and then rebase master:

Rebasing code changes from master to dev
Rebasing code changes from master to dev

If we now see git commit history for newQuickFix branch, it would be something like this:

Commit history for newQuickFix branch after rebase
Commit history for newQuickFix branch after rebase

Observe Effects of Git Rebase

If you carefully compare the commit history before and after rebase, you can see that the SHA hash for commit has now changed for those two commits even though message remains the same. Also, if you now compare the commit history for all branches, you can see it is only one single line now:

Commit history for all branch after rebase
Commit history for all branch after rebase

To further make point that previous commits still linger in the git memory only there are no branches pointing to it, we’ll do git reset to previous commit f94a668:

Accessing commits created before rebase
Accessing commits created before rebase

Note that it has recognized previous commit hash fine.

Perform Rebase on same branch

Now for this, we’ll squash the two commits f94a668 and aa9bddc into single one. We can simply run below command for this:

git rebase -i HEAD~2
Rebase changes on the same branch
Rebase changes on the same branch

Now, if we observe git history, it will be like this:

Observe commit history after rebase

As its clear from above, it merged those two commits into one single commit and also changed the SHA hash for the new commit.

In next blog post in this series, we’ll learn how to perform git rebase using Visual Studio.

One thought on “Working with Git and Visual Studio – Use git rebase with git command line

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s