本文翻译自:What's the difference between 'git merge' and 'git rebase'?

git mergegit rebase什么区别?


#1楼

参考:https://stackoom.com/question/17vbt/git-merge-和-git-rebase-有什么区别


#2楼

Suppose originally there were 3 commits, A , B , C : 假设最初有3个提交, ABC

Then developer Dan created commit D , and developer Ed created commit E : 然后,开发人员Dan创建了提交D ,而开发人员Ed创建了提交E

Obviously, this conflict should be resolved somehow. 显然,这种冲突应该以某种方式解决。 For this, there are 2 ways: 为此,有两种方法:

MERGE : 合并

Both commits D and E are still here, but we create merge commit M that inherits changes from both D and E . 提交DE都仍在这里,但是我们创建了合并提交M ,它继承了DE更改。 However, this creates diamond shape, which many people find very confusing. 但是,这会产生菱形 ,许多人感到非常困惑。

REBASE : REBASE

We create commit R , which actual file content is identical to that of merge commit M above. 我们创建提交R ,其实际文件内容与上面的合并提交M相同。 But, we get rid of commit E , like it never existed (denoted by dots - vanishing line). 但是,我们摆脱了提交E ,就像它不存在一样(用点表示-消失线表示)。 Because of this obliteration, E should be local to developer Ed and should have never been pushed to any other repository. 由于这种淘汰, E应该在开发人员Ed本地,并且永远不要被推送到任何其他存储库。 Advantage of rebase is that diamond shape is avoided, and history stays nice straight line - most developers love that! 变基的优点是避免了钻石形状,并且历史沿直线一直很好-大多数开发人员都喜欢这样做!


#3楼

I really love this excerpt from 10 Things I hate about git (it gives a short explanation for rebase in its second example): 我真的很喜欢我对git讨厌的10件事的摘录(它在第二个示例中简要介绍了rebase):

3. Crappy documentation 3.糟糕的文档

The man pages are one almighty “f*** you” 1 . 手册页是一个全能的“ f *** you” 1 They describe the commands from the perspective of a computer scientist, not a user. 它们从计算机科学家而不是用户的角度描述命令。 Case in point: 例子:

 git-push – Update remote refs along with associated objects 

Here's a description for humans: 这是人类的描述:

 git-push – Upload changes from your local repository into a remote repository 

Update, another example: (thanks cgd) 更新,另一个示例:(感谢cgd)

 git-rebase – Forward-port local commits to the updated upstream head 

Translation: 翻译:

 git-rebase – Sequentially regenerate a series of commits so they can be applied directly to the head node 

And then we have 然后我们有

 git-merge - Join two or more development histories together 

which is a good description. 这是一个很好的描述。


1. uncensored in the original 1.未经审查的


#4楼

Personally I don't find the standard diagramming technique very helpful - the arrows always seem to point the wrong way for me. 我个人认为标准图表绘制技术不是很有用-箭头似乎总是为我指出错误的方向。 (They generally point towards the "parent" of each commit, which ends up being backwards in time, which is weird). (它们通常指向每个提交的“父”,最终导致时间倒退,这很奇怪)。

To explain it in words: 用语言解释:

  • When you rebase your branch onto their branch, you tell Git to make it look as though you checked out their branch cleanly, then did all your work starting from there. 当你衍合分支到自己的分公司,你让Git使它看起来好像你清晰地检查了自己的分支,然后做所有的工作从那里开始。 That makes a clean, conceptually simple package of changes that someone can review. 这样一来,干净的,概念上很简单的变更包就可以供他人查看。 You can repeat this process again when there are new changes on their branch, and you will always end up with a clean set of changes "on the tip" of their branch. 当其分支上有新更改时,您可以再次重复此过程,并且最终总会在其分支的“尖端”获得一组干净的更改。
  • When you merge their branch into your branch, you tie the two branch histories together at this point. 当您将其分支合并到分支中时,此时将两个分支历史联系在一起。 If you do this again later with more changes, you begin to create an interleaved thread of histories: some of their changes, some of my changes, some of their changes. 如果以后再进行更多更改,将开始创建历史的交错线程:它们的某些更改,我的某些更改,它们的某些更改。 Some people find this messy or undesirable. 有些人觉得这很杂乱或不受欢迎。

For reasons I don't understand, GUI tools for Git have never made much of an effort to present merge histories more cleanly, abstracting out the individual merges. 由于我不明白的原因,用于Git的GUI工具从没有做出太多的努力来更清晰地呈现合并历史,从而抽象出各个合并。 So if you want a "clean history", you need to use rebase. 因此,如果您需要“干净的历史记录”,则需要使用rebase。

I seem to recall having read blog posts from programmers who only use rebase and others that never use rebase. 我似乎还记得曾经阅读过仅使用rebase和从未使用rebase的程序员的博客文章。

Example

I'll try explaining this with a just-words example. 我将尝试用一个简单的例子来解释这一点。 Let's say other people on your project are working on the user interface, and you're writing documentation. 假设您项目中的其他人正在使用用户界面,而您正在编写文档。 Without rebase, your history might look something like: 没有重新设置基准,您的历史记录可能类似于:

Write tutorial
Merge remote-tracking branch 'origin/master' into fixdocs
Bigger buttons
Drop down list
Extend README
Merge remote-tracking branch 'origin/master' into fixdocs
Make window larger
Fix a mistake in howto.md

That is, merges and UI commits in the middle of your documentation commits. 也就是说,合并和UI提交位于文档提交的中间。

If you rebased your code onto master instead of merging it, it would look like this: 如果您将代码重新基于master而不是合并,则它看起来像这样:

Write tutorial
Extend README
Fix a mistake in howto.md
Bigger buttons
Drop down list
Make window larger

All of your commits are at the top (newest), followed by the rest of the master branch. 您的所有提交都位于顶部(最新),然后是master分支的其余部分。

( Disclaimer: I'm the author of the "10 things I hate about Git" post referred to in another answer ) 免责声明:我是另一个答案中提到的“我讨厌Git的十件事”的作者


#5楼

While the accepted and most upvoted answer is great, I additionally find it useful trying to explain the difference only by words: 尽管公认的最受好评的答案是不错的,但我发现仅用文字来解释区别也很有用:

merge 合并

  • “okay, we got two differently developed states of our repository. “好吧,我们的存储库有两种不同的开发状态。 Let's merge them together. 让我们将它们合并在一起。 Two parents, one resulting child.” 两个父母,一个孩子。”

rebase 重新设定

  • “Give the changes of the main branch (whatever its name) to my feature branch. “将主分支(无论其名称如何)的更改都交给我的功能分支。 Do so by pretending my feature work started later, in fact on the current state of the main branch.” 假装我的特征工作是在以后开始的,实际上是在主分支的当前状态下进行的。”
  • “Rewrite the history of my changes to reflect that.” (need to force-push them, because normally versioning is all about not tampering with given history) “重写我的更改历史以反映这一点。” (需要强行推动它们,因为通常版本控制就是篡改给定的历史)
  • “Likely —if the changes I raked in have little to do with my work— history actually won't change much, if I look at my commits diff by diff (you may also think of 'patches').“ “就像—如果我所进行的更改与我的工作几乎没有关系—如果我逐个查看我的提交(您也可能会想到'补丁'),历史实际上将不会有太大变化。”

summary: When possible, rebase is almost always better. 摘要:可能的话,变基几乎总是更好。 Making re-integration into the main branch easier. 使重新集成到主分支变得更加容易。

Because? 因为? ➝ your feature work can be presented as one big 'patch file' (aka diff) in respect to the main branch, not having to 'explain' multiple parents: At least two, coming from one merge, but likely many more, if there were several merges. feature您的要素作品可以相对于主分支显示为一个大的“补丁文件”(又称为diff),而不必“解释”多个父级:至少两个,来自一次合并,但如果有的话,可能更多是几次合并。 Unlike merges, multiple rebases do not add up. 与合并不同,多个基准不会累加。 (another big plus) (另一大优点)


#6楼

Git rebase is closer to a merge. Git rebase更接近合并。 The difference in rebase is: rebase的区别是:

  • the local commits are removed temporally from the branch. 本地提交会暂时从分支中删除。
  • run the git pull 运行git pull
  • insert again all your local commits. 再次插入所有本地提交。

So that means that all your local commits are moved to the end, after all the remote commits. 因此,这意味着在所有远程提交之后,所有本地提交都将移至末尾。 If you have a merge conflict, you have to solve it too. 如果您有合并冲突,则也必须解决它。

#39;git merge#39;和#39;git rebase#39;有什么区别?相关推荐

  1. 4. git merge简介,以及git merge的参数–ff、–no-ff和–squash的区别?

    1.git merge原理 A---B---C topic/ \D---E-----F------H master 在master分支中使用git merge topic,只会将C和H中的文件进行比较 ...

  2. 【Git】Git 分支管理 ( 创建并切换分支 | 查看分支 git branch | 合并分支 git merge dev | 删除分支 git branch -d dev )

    文章目录 一.创建并切换分支 1.创建分支 git branch dev 2.切换分支 git checkout dev 3.创建并切换分支 git checkout -b dev 二.查看分支 gi ...

  3. git merge origin master和git merge origin/master的区别

    目录 背景 问题 探究 背景 某天同事小L找我帮忙解决代码冲突引起的问题,折腾一番之后,问题解决了.场景是他想把最新的主干分支代码合并到当前开发分支上,解决掉冲突,然后提交推送到远程.他合并代码的过程 ...

  4. 你什么时候使用git rebase而不是git merge?

    什么时候建议使用git rebase与git merge ? 成功改造后我还需要合并吗? #1楼 在合并/ rebase之前: A <- B <- C [master] ^\D <- ...

  5. git merge 冲突_更新了!深入浅出图解Git,入门到精通(保姆级教程)第三篇

    原文链接:https://mp.weixin.qq.com/s/d7YwRi1mEkxUSUqxWD_B1Q 这一篇写完基本Git的操作就图解完了,如果想深入了解Git,这里可以推荐一些Git的硬核书 ...

  6. git merge和git merge --no-ff有什么区别?

    本文翻译自:What is the difference between `git merge` and `git merge --no-ff`? Using gitk log , I could n ...

  7. git fetch - git merge - git pull 指令

    git fetch - git merge - git pull 指令 Incorporates changes from a remote repository into the current b ...

  8. git pull origin master与git pull --rebase origin master的区别

    建议:最好看一遍廖雪峰的git教程,看完时间差不多就两个小时,git相关的知识写的很清楚,看完整个人都豁然开朗,很多git的问题都想通了. 区别: git pull=git fetch + git m ...

  9. 是否有“他们的”版本的“git merge -s ours”?

    当使用git merge将主题分支"B"合并到"A"时,我会遇到一些冲突. 我知道使用"B"中的版本可以解决所有冲突. 我知道git mer ...

  10. git merge的用法

    现在我们有一个主分支dev,现在有一些feature分支,需要偶尔将dev分支的稳定代码合入feature分支,所以用到git merge 现在整理下git merge的几个概念和用法 Fast fo ...

最新文章

  1. windows安装ngnix
  2. 列表逆序排序_Python零基础入门学习05:容器数据类型:列表和元组
  3. WebKit 内核源码分析 (三) Page
  4. Windows Update Error: 80244019
  5. 蛋壳公寓回应破产传闻:没有破产 也不会跑路
  6. 32岁男子沉迷网游十年离世 临终称真有意思
  7. Introducing Blackle, the energy saving search
  8. python上下文管理器ContextLib及with语句
  9. UVa 10950 - Bad Code
  10. python并发编程方法_一文了解Python并发编程的工程实现方法
  11. DevExpress DXperience历史版本下载地址大全
  12. VSTO程序基本知识(二)
  13. oracle数据库常用操作语句大全,Oracle 数据库常用操作语句大全
  14. 苹果手机怎么查看已连接的wifi密码_如何查看已连接的WiFi密码,可以这样做!...
  15. 安装CAD2006出现html,win10系统安装cad2006出现已终止CAd2006-simplifieng安装的设置教程...
  16. ValueError: Cannot assign “<...>“: the current database router prevents this relation.
  17. win快捷键_终于找到了!10个Win+组合快捷键,让你的工作效率提升10倍
  18. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java小微企业ERP软件物料需求模块设计55ss9
  19. JPG图片插入到CAD图纸中的2种方法
  20. 快速切换IP的批处理!

热门文章

  1. GreenDao 工具类 --- 使用 Json 快速生成 Bean、表及其结构,炒鸡快!
  2. Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row 1
  3. win7配置iis 出现:HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容...
  4. 在MyEclipse中配置Tomcat服务器
  5. Delphi中常用字符串处理函数
  6. jquery广告轮播插件
  7. 简书全站爬取 mysql异步保存
  8. 微信公众号 分享接口 签名通过 分享无效果(JSSDK自定义分享接口的策略调整)...
  9. easyUI之ComboBox(下拉列表框)
  10. python(day04)