git merge和git rebase的區別

合併兩個commit有兩種方式:一種是 git merge ,而這一種在git pull命令中是默認的   另一種是 git rebase


git pull命令中

git pull <遠程主機名> <遠程分支名>:<本地分支名>

例如:git pull origin next:master

把遠程origin分支next fetch回來並git merge

git pull --rebase <遠程主機名> <遠程分支名>:<本地分支名>

而如果使用rebase方式則

git pull --rebase origin next:master

這裏建議還是先git fetch在做決定是git merge還是git rebase吧,如果只是git pull則默認爲merge。


現在看看merge和rebase的區別:

引用一個文章:http://stackoverflow.com/questions/16666089/whats-the-difference-between-Git-merge-and-git-rebase


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!

講的真好,真棒,32個贊。

diamond 菱形

get rid of 刪除

identical 相同的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章