Git Rebasing in No harms Way =D

Git is a useful tool for Version control. Git Rebasing is used when we have a scenario like we are working in  a feature branch and   not taken the pull within the last 3 days . Also the remote has 3 more commits we don’t have in our feature branch. Also the Feature branch has 5 commits. You need to push the feature branch  without loosing the  existing commits  in master and and feature branch  too.

Consider that  you have 3 commits in master.From that branch ,create  1 branch named feature_branch . After that your co workers push a commit named 4 to remote .But that commit is not available in feature_branch. Also you have created  2 more commits in your feature branch namely 5 and 6. Now what you need to do is merge  commit no 4 to your feature branch . Then only you can able to push  your commits to remote .

rebase1

We have a git command called git merge origin/master is for this purpose. But the problem with this command is that it will create a new commit as merge commit . what you need to get that commit from remote without  a merge commit. For that we can use this

git rebase

rebase2

Example 1 : using git Merge

rebase3

create an another branch named test and checkout to that branch

rebase4

add some more commits to that local branches.

rebase5

rebase6

now checkout to master and add a new commit there..

rebase7

now switch to your local branch and do the command

git merge master

.rebase8

there is an additional merge commit is present along with other commits .Thats can be avoided by using the

git rebase

Example 1 : using git rebase.

create new branch named rebase_ex and add some commits there.

rebase9

now the commits in rebase_ex are ..

rebase10

now checkout to master and the master commits are

rebase11

now create some new commits there

rebase 13

total commits in master are

rebase14

now checkout to your feature branch and do the

git  rebase master

rebase15

so the total commits currently in your local branch is

master commits + your feature branch commits too.

.rebase16

Tada.. all done . you are now expert in the git rebase . one thing you need to understand is ; there is no merge commit is now created .

 

 

 

Advertisements

One Comment Add yours

  1. Good article Anoob Bava!

    Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s