How To Use Git Rebase Carefully
Posted By : Shiv Pujan Maurya | 27-Jan-2018
Note: Please use these commands only when you know what you are doing, some commands could be dangerous if used without proper understanding.
Merging and Rebasing
While using git sometimes we reach in a situation where something is broken and we are in hurry to fix it out immediately. General git commands don’t provide functionality to handle tasks like rebasing, changing logs and their order and merging the branch to master but not rewriting the old log. Without wasting much time let’s move forward.
Merging VS Rebasing
Merging:-
Let’s suppose that you are working on a new functionality to be added to your project. Consider that you have two branches the master branch and dev branch. The master branch contains production code while new features are added to the dev branch.
After you are done with development you want the code to be added to the master branch. This could be done using
git merge master dev
This add together the histories of both branches and doesn't break your code at all.
This process has to be followed every time you add a new code to your dev branch. What if master is very active then it may be hard to manage the history of both branches that leads to an overhead. Now rebase comes as a useful feature here.
Rebasing :-
An alternate method to overcome this issue is using rebasing command.
git checkout dev
git rebase master
This moves the dev branch to the beginning of master and re-writes the commit history.
Advantages of rebasing: cleaner history, linear commit records, makes it easy to navigate with logs by using git log, git bisect etc.
Disadvantages: re-written history may lead to confusion, and you lose context provided by git merge.
This problem of losing history and rewriting commit can be solved using Interactive rebasing.
Interactive Rebasing can solve this by giving us option to control over what goes to dev branch from master. To start this just pass -i option with rebase
git checkout dev
git rebase -i master
This opens a text editor with list of commits and their messages where you can edit what you don’t find important.
pick 4nf423o Message for commit #6
pick 3f8j03d Message for commit #7
pick 42b3wb Message for commit #8
Here you can change the sequence of commits as well. When you are done just save the file and the changes will be written accordingly. This feature provides a better history management. Git merge cannot to this anyway.
Now that we have understood rebasing let’s discuss when not to use rebasing.
Rethink before you use git rebase while working with public branches:-
while working with public branches its good idea not use git rebase. Consider that you rebase the dev branch to master then all the history will be re-written but this is for you only. All other developers would not find these changes. This will require you to again merge this with the master that means git will find that your history is divergent than the other developers master branch. This creates a messy situation.
If you try to push these changes to remote repo then git will prevent you doing this. But still, you can do this by using git push --force option.
git push --force
This re-writes the remote repo commit history as per your updates which creates mess for other developers.
Using git rebase for code cleanup :-
While working with new development you can use rebase to cleanup code locally before sending it to remote repo. Rebase provides options to move to specific branch or to specific commit. For example you can rebase last 5 commits by using git checkout dev and git rebase -i HEAD~5
git checkout dev
git rebase -i HEAD~5
By specifying HEAD~3 you are not actually changing the branch you are just rewriting last 5 commits that follow this. This will not incorporate changes into dev branches.
If you want to rewrite entire dev use git merge-base dev master
git merge-base dev master
Following all this will bring a new clean and clear code which will lead to well finished and documented git code.
Using git rebase to merge your local code to other developer’s local code without merging to master :-
Suppose you want your send your code in other developer but without sending it into master branch and vice versa. Here git rebase comes handy just use --rebase with git pull (by default git pull will perform merge but --rebase avoids this)
git pull --rebase
Integrating an approved change your to master branch or any temporary-branch ( if you are not comfortable with master)
git checkout dev
git checkout -b temp-branch
git rebase -i master
git checkout master
git merge temp-branch
Summary:- what we did we learn above
We understood concept of rebasing. Then we learnt how git rebase can help us in creating a clear code base and better finished product. Now this is up to your confidence whom you stick with git merge or it rebase.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Pujan Maurya
Shiv is a Redhat certified engineer with B.Tech in Computer Science & Engineering. He is passionate about new technologies specially in DevOps and Artificial intelligence.