Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between 'git merge' and 'git rebase'?

What's the difference between git merge and git rebase?

like image 291
Daniel Peñalba Avatar asked May 21 '13 09:05

Daniel Peñalba


People also ask

Which is Better git rebase or git merge?

Git Merge and Git Rebase are both used to combine the changes of branches but in a distinct way. Git Merge - For developers using version control systems, merging is a prevalent method. Merging takes the contents of a source branch and combines them with a target branch, to be more precise.

What does rebase mean in git?

What is git rebase? Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow.

What is the difference between git pull and git merge?

The git pull command first runs git fetch which downloads content from the specified remote repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.


1 Answers

Suppose originally there were 3 commits, A,B,C:

A-B-C

Then developer Dan created commit D, and developer Ed created commit E:

A-B-C-D-E

Obviously, this conflict should be resolved somehow. For this, there are 2 ways:

MERGE:

A-B-C-D-E-M

Both commits D and E are still here, but we create merge commit M that inherits changes from both D and E. However, this creates diamond shape, which many people find very confusing.

REBASE:

A-B-C-D-E-R

We create commit R, which actual file content is identical to that of merge commit M above. But, we get rid of commit E, like it never existed (denoted by dots - vanishing line). Because of this obliteration, E should be local to developer Ed and should have never been pushed to any other repository. Advantage of rebase is that diamond shape is avoided, and history stays nice straight line - most developers love that!

like image 130
mvp Avatar answered Oct 14 '22 18:10

mvp