Using Git Rebase
Working on a cool project, doing cool and unusual stuff has its own charms and challenges. For doing cool things, you need cool tools to play with and Git surely is one of the coolest ones around. One of the many powerful features of git is rebase.
While working on a large project, you almost always develop your feature on a new branch, and for a large enough feature/requirement, It could take up to month or more to deliver it.
Lets consider a scenario where you need to begin on a new feature and you create a separate branch,(coolNewStuff) , off your main integration branch, (master).
While you were working on the stuff, other guys in your team were pushing and integrating their stuff into the master branch. As a result master branch moved ahead and there were loads of commits and other stuff added to it.
Now before pushing your cool stuff into master, you would need to pull in the changes in the master to your current(coolNewStuff) branch. You could easily do that using
[code]
git pull origin master
[/code]
Resolve conflicts. commit changes and merge this branch with master.
Nothing wrong with that actually, except that it creates a lot of intermingled commits in the branch, and it won’t give a clear roll back point.
What you actually want to do is put your work on top of the existing master branch as a separate commit so that you get a clear roll back point in case something goes wrong.
You want your changes of coolNewStuff branch on top of current master head, you don’t want master on top your coolNewStuffBranch.
Enter Rebase.
What rebase allows you to do is rewrite the history of your branch. You can actually pull in the changes from master branch place them above the master branch, you branched out off (some 30-45 days back) and then place all the stuff you developed (all commits you made since the day you branched off) on top of this new pulled in changes from master. So, this looks like…
All you need to do is on the coolNewStuff branch call
[code]
git rebase master
[/code]
This will start your rebasing process. It will basically collect the changes you made since you branched out of master to another temporary location, merge the old coolNewStuff (which was same as the master at the time you branched off) with the new changes on the master and then places your changes on top of it.
This process rewrites the history of the branch.
If you get conflicts. resolve the conflicts and add the files back
[code]
git add fileName
[/code]
Continue with rebase
[code]
git rebase –continue
[/code]
Do a git log and see that your changes are on top of the latest changes of master branch.
Now You won’t be able to push the changes to coolNewStuff branch as the history of branch has been rewritten.
On doing git status you would get a message saying
Your branch and ‘origin/coolNewStuff’ have diverged
You will need to remove your remote branch
[code]
git push origin :coolNewStuff
[/code]
and push again. Do remember to notify other guys who are working on the coolNewStuff branch to delete their local coolNewStuff branch and fetch the new coolNewStuff branch.
Now when you merge this branch with master you will see that your changes are on top of the recent stuff on master and you can easily roll back the stuff in case something goes wrong.
Hope this helps.
Sachin Anand
email : sachin[at]intelligrape[dot]com
twitter : @sachin__anand