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:
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:
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:
If we now observe the git history for all branches, it would be like this:
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:
If we now see git commit history for newQuickFix branch, it would be something like this:
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:
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:
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
Now, if we observe git history, it will be like this:
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”
[…] 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 […]