Rebasing is an alternate solution for the merging where rebasing differs by rewriting the commit history in order to produce a straight, linear succession of the commits.
When you rebase, Git finds the base of your branch and finds all the commits between the Head and the Base and replays those commits by pushing back to it.
Now, let us understand step by step how the Rebase will work, follow the steps as below. Here am working with my Git repository called starter-web-1/, which I have forked from scm-ninja/starter-web
Let's go to the Git Bash terminal, so am in my project folder and now am changing into my git repositories working directory
cd starter-web-1/ , If you do an
ls, you can see the number of files which are part of this project.
git status, you can see that we are on the clean working directory.
git branch, here we have our master only.
Let us create some feature branch and do some work on it and then switch back to the master branch.
And also we are going to do some work in the master branch and then rebase the changes back into the feature branch. Here I am going to create a new feature branch called myfeature and switching to that branch
git checkout -b myfeature.
git status, Git tells that we have a modified file.
git commit -am "saying thanks to all my students".
git checkout master
notepad++ README.mdand add some text towards the top as Adding the line for rebasing example save then exit.
git status, Git tells us that we have modified file called README.md.
git commit -am "Adding online to the README file for rebase example"
git log --oneline --decorate --all --graph, here you can notice that the master and the feature branches are in a separate line and you can also see that the graph line is diverging.
git checkout myfeature
git rebase master, and press enter and it will return that rebasing is re-writing our head, which is our current branch and we can re-apply any changes that we have made on the master branch.
git log --oneline --decorate --all --graph, you can see that we have master applied and then the changes to the feature applied.
In order to make that happen, we have to rewind the change that happened on the myfeature and playback the changes in master on the myfeature branch.
And then apply the change that happened on the myfeature branch originally, in doing so it will be flattened our history. It is also going to allow us to fast-forward merge once we're done making changes on myfeature.
Let's make another modification on the README.md file before doing merge, We have the version of the README.md file on master, edit it by adding some text under Starter-web-project expand the paragraph by separating it with a comma save then exit.
git status, which tells me that, I am on the myfeature branch and have one modified file called README.md
git commit -am "Adding another change after rebase".
git log --oneline --decorate --all --graph, you can see that the myfeature branch has moved a couple of commits ahead of the master branch with our last commit at the top.
git checkout master.
git status, tells me that we are the master branch, head with one commit.
git diff master myfeature, so this compares the master branch with the myfeature branch.
git merge myfeature, you can see that we have ended up with the Fast Forward merge.
git log --oneline --decorate --all --graph, you can see that the master is now caught up with the myfeature branch.
git branch -d myfeature
We have learned the basic concept of Rebasing, now let us know how to set up Rebase conflict.
ls, you will see the list of files which are part of this project.
git commit -am "Mb before rebase conflicts", here Mb stands for the master branch.
git checkout -b bigtrouble
git commit -am "fb Adding some trouble to simple.html", Here fb stands for feature branch.
git checkout master
git commit -am "fb conflicting changes brewing".
git status, we have a clean working directory.
git log --oneline --graph --decorate --all
We will have a look at how to abort the Rebase.
git checkout bigtrouble
git difftool master bigtrouble
git rebase master, as expected rebase resulted in the conflict.
git rebase --abortand we are completely out of the rebase.
git status, we are still on the bigtrouble branch with a clean working directory.
git log --oneline --graph --decorate --all, You can see that nothing has changed and we still have the two branches that have diverged.
In the last section, we learned about how to abort the rebase if it is complicated to resolve, let us try to resolve the rebase conflicts using mergetool.
git status, Git tells that we are on a bigtrouble branch with a clean working directory.
git log --oneline --graph --decorate --all, you can see that we have two branches which are diverged.
git rebase master, so as expected we are in the conflicting state during the rebase.
git mergetool, that will launch the visual mergetool that's configured with Git.
git status, you can see that you have simple.html as a modified file.
git add simple.html
git status, Git tells that, you have fixed the conflicts and also it will give some suggestions.
git rebase --continue, thus, we had resolved the rebase conflicts.
git log --oneline --decorate --graph --all, you can see that we have our master commit and then bigtrouble commit.
git commit -am "Adding changes after rebasing conflict"
git status, we are on the clean working directory on a bigtrouble branch.
git log --oneline --graph --decorate --all, you can see that we have two commits ahead of the master with our latest commit at the top.
git checkout master, thus we switched to the master branch.
git merge bigtrouble, It will result in Fast Forward merge.
git log --oneline --graph --decorate -- all, you can see that the master and bigtrouble pointing to the same commit and that the branching graph is linear as well.
Till now, you have learned different operations of rebasing on Local system, now let us understand how to rebase the Remote branches.
git pull origin master.
git push origin master, hence it should be synchronized.
git status, Git tells that we are up-to-date with origin/master, with a clean working directory.
git commit -am "Local: updating simple.html copyright notice".
git status, Git tells us that we are on a clean working directory with one commit ahead on origin/master.
git commit -am "Remote: minor change to the index.html"and then click on the commit changes, which is written in green color.
git status, Git tells that, we are just one commit ahead because the master branch is a local branch and it will indicate only local commits here.
git fetch origin master.
git status, Git tells that the origin/master and local branches have diverged.
git pull --rebase origin masterand press enter.
git status, we can see that our branch is ahead of origin/master by one commit.
git log --oneline --graph --decorate --all, we can see that we have origin/master and then our master branch.
We have reached the end of the Rebase section, So, just do a bit of clean up over here.
git status, Git tells us that we are on a master branch with a clean working directory with one commit ahead of origin/master.
git branch -d bigtrouble.
git status, Git confirms that bigtrouble branch has deleted.
git pull origin master, as expected, It replies as we are already up-to-date.
git push origin master