2

At what exact scenario I have to use Git Rebase ?

I am trying to understand this for the past three years but i could'nt. Have read many blogs, stackoverflow's Q&A about git rebase but i am not clear about that still. I know, this might be the duplicate question but i need to understand exactly what is that git rebase and when to use that.

Anyone, Please give me clear explanation. I have read the following lines in one of the blogs

"In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase."

I know what to do with the merge command. but, how rebase replaces merge command? Give me any flow of commands by explaining this is how the rebase replaces merge command. I think i can understand in such a way.

Sorry for my poor English.. Thanks in Advance.

Breen ho
  • 1,601
  • 14
  • 23
  • It's explained in every git book: http://git-scm.com/book/en/Git-Branching-Rebasing. – JB Nizet Jul 09 '14 at 10:16
  • possible duplicate of [What's the difference between 'git merge' and 'git rebase'?](http://stackoverflow.com/questions/16666089/whats-the-difference-between-git-merge-and-git-rebase) – mvp Jul 09 '14 at 10:16
  • @JBNizet., Thanks for your comment! That is what the blog i mentioned in my question. Read the first line in the blog you have shared. Could you please write flow of commands which replace merge?? – Breen ho Jul 09 '14 at 10:32
  • @mvp., Yeah I know this is a duplicate question. I did not understand exactly what is git rebase by going through the blogs and stackoverflow Q&A. It would be better if it can be explained with flow of git commands that how rebase replaces merge. – Breen ho Jul 09 '14 at 10:35
  • Rebase does not replace merge (or other way around). They are both valid ways to solve the same problem - resolving forked history. Linked answer explains it in detail – mvp Jul 09 '14 at 10:47
  • Perhaps being too literal, but you never *have* to use rebase. There are times when you maybe *should prefer* to use rebase, but it's never a hard requirement. – twalberg Jul 10 '14 at 14:44

2 Answers2

1

This interactive tutorial is very good, try to do a few rebase lessons. I saw that few people actually understood rebasing learning from it.

Rebase does not replace merge, merge is to create new commit by "joining" few. Rebase is generally to moving commits around the tree, switching commits order and that stuff.

spinus
  • 5,497
  • 2
  • 20
  • 26
  • Thanks @spinus..!! Its pretty much clear to understand about rebase.. Also, I tried to do exercise from my own git repo by creating multiple branches. Thanks once again. – Breen ho Jul 10 '14 at 12:49
  • -1 totally unhelpful link-only answer. When you say "switching commits order and that stuff", it means you have not any idea how it really works. And for the record, rebase can replace merge - as long as local commits to be rebased were never pushed upstream. – mvp Jul 29 '14 at 10:02
  • @mvp, would be nice to read whole sentence not only one part. Provide better answer and I'm happy to upvote. – spinus Jul 29 '14 at 22:55
0

Based on my understanding, This is what the rebase does.

Scenario Explained:
1) I am currently in branch master.
2) Create branch A from master
3) Also, Create branch B from master
4) I let Developer named A to work on branch A. Dev A modifies code in branch A.
5) I let Developer named B to work on branch B. Dev B modifies code in branch B.
6) Now, Dev B thinks that he wants to merge Dev A's code right from being in his own branch branch B. (instead of doing checkout to branch A, pull the code from branch A, again checkout to branch B and do a git merge branch A. this is what git merge does ).
7) But, when Dev B runs "git rebase branch A" by being in his own branch, the head of branch B comes on top by merging branch A with branch B.
8) Now, if the Dev A thinks that he wants to merge Dev B's code. He has to do "git rebase branch B". Now the head of branch B will be on top of the head by merging B's code (already merged with branch A) to branch A.
9) Now, flow of commit version will be in straight line. this is clean!!

Please correct me if my wrong.. Interested in learning more about GIT..

Thanks

Breen ho
  • 1,601
  • 14
  • 23