文档章节

Git Merge vs Git Rebase

Kolosek
 Kolosek
发布于 2018/05/28 17:11
字数 608
阅读 10
Git">收藏 0

Git merge and rebase serve the same purpose – they combine multiple branches into one. Although the final goal is the same, those two methods achieve it in different ways. Which method to use?

What Does Merge or Rebase Mean?

Here we have a sample repository that has two diverging branches: the master and the feature. We want to blend them together. Let's take a look how these methods can solve the problem.

git-flow

Merging

When you run git merge, your HEAD branch will generate a new commit, preserving the ancestry of each commit history.

git-merge-2

Fast forward merge is a type of merge that doesn't create a commit, instead, it updates the branch pointer to the last commit.

Rebasing

The rebase re-writes the changes of one branch onto another without creating a new commit.

For every commit that you have on the feature branch and not in the master, a new commit will be created on top of the master. It will appear as if those commits were written on top of master branch all along.

git-rebase

Merging Pros and Cons

Pros:

  • Simple to use and understand.
  • Maintains the original context of the source branch.
  • The commits on the source branch are separated from other branch commits. This can be useful if you want to take the feature and merge it into another branch later.
  • Preserves your commit history and keeps the history graph semantically correct.

Cons:

  • History can become intensely polluted by lots of merge commits because multiple people are working on the same branch in parallel. Visual charts of your repository are can become a mess and don't add too much information. It can look like a London Tube Map Commit!

london-tube-map-commit

Rebasing Pros and Cons

Pros:

  • Code history is simplified, linear and readable.
  • Manipulating a single commit history is easier than a history of many separate feature branches with its additional commits.
  • Clean, clear commit messages make it better to track a bug or when a feature was introduced. Avoid polluting history with 20+ single-line commits!

Cons:

  • Lowering the feature down to a handful of commits can hide context.
  • Rebasing doesn't work with pull requests, because you can't see what minor changes someone made. Rewriting of history is bad for teamwork!

You need to be more careful with rebasing than when merging.

  • It requires more work when dealing with conflicts. Using rebase to keep your feature branch updated requires that you resolve similar conflicts again and again. While with merging, once you solve the conflicts, you're set. You have to resolve the conflict in the order they were created to continue the rebase.

Which Method to Choose?

When your team uses a feature based workflow or is not familiar with rebase, then git merge is the right choice for you:

  • It allows you to preserve the commit history for any given feature while not worrying about overriding commits and changing history. It helps you avoid unnecessary git reverts or resets!
  • Different features remain isolated and don't interfere with existing commit histories.
  • Can help you re-integrate a completed feature branch.

On the other hand, if you value more a clean, linear history then git rebase may be most appropriate. You will avoid unnecessary commits and keep changes more centralized and linear!

If you rebase incorrectly and unintendedly rewrite the history it can lead to serious issues, so make sure you know what you are doing!

Rebase or merge, what is your preference? Here in Kolosek, we lean more towards merge for our feature-based workflow.

This article is originally published on Kolosek Blog.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
关于git rebase和git merge

最近在研究git rebase和git merge的用法,发现其实也挺简单的。(T_T人家还研究了好久… git merge 应该大家都比较熟悉,就是把branch merge进来; git rebase 的意思其实也是蛮简单的,就是r...

mingkaidox
2013/07/04
7.5K
4
git rebase vs git merge

语法为git rebase [base], base就是commit id或者ref名称 比如git fetch; git rebase origin/master 就是将自己自clone或上次pull以来所有的自己所做的的commit(尚未push的)放在[base]之后...

cyper
2015/08/24
79
0
git rebase VS git merge? 更优雅的 git 合并方式值得拥有

写在前面 如果你不能很好的应用 Git,那么这里为你提供一个非常棒的 Git 在线练习工具 Git Online ,你可以更直观的看到你所使用的命令会产生什么效果另外,你在使用 Git 合并分支时只会使用...

tan日拱一兵
07/16
73
0
git merge 与 git rebase的区别

前言 其实这个问题困扰我有一段时间,相信也有人和我一样有这个困扰,网上已有很多这种解释了,但是要么就是无图,要么就是解释的很乱,没太看懂,经过自己对git的使用,加上向同事请教,算是...

天王盖地虎626
02/04
85
0
Egit - Cannot checkout now - stuck in rebase state Ask

使用rebase时发生冲突,解决冲突后不知道怎么进行下去,下面链接很好的解决方法 https://stackoverflow.com/questions/34000319/egit-cannot-checkout-now-stuck-in-rebase-state 这是在sta...

wangwei2134
2018/04/13
0
0

没有更多内容

加载失败,请刷新页面

加载更多

linux 磁盘不足异常

linux 报 No space left on device 异常 ,则是磁盘不足 ,导致异常 运行 df -h 命令查询磁盘使用率,如果有100%,则查找目录大日志文件删除 1.磁盘不足导致系统应用写入文件失败,如系统日志...

zaolonglei
26分钟前
3
0
即学即用的 30 段 Python 实用代码

☞ 分享:最全最新的Python学习大礼包 ☜ 点击查看 编译:Pita & AI开发者,作者:Fatos Morina Python是目前最流行的语言之一,它在数据科学、机器学习、web开发、脚本编写、自动化方面被许...

Object_Man
26分钟前
3
0
The server time zone value 'EDT' is unrecognized or represents more than one time zone.

2019-10-14 18:07:43.714 ERROR 74363 --- [Druid-ConnectionPool-Create-1855026648] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://10.30......

yizhichao
40分钟前
8
0
html加载顺序以及影响页面二次渲染额的因素

本文转载于:专业的前端网站➱html加载顺序以及影响页面二次渲染额的因素 浏览器请求发往服务器以后,返回HTML页面,页面内容开始渲染,具体的执行顺序为: 1. 浏览器开始载入html代码,发现<...

前端老手
42分钟前
9
0
BeginnersBook JSP、JSTL、Servlet 教程

来源:ApacheCN BeginnersBook 翻译项目 译者:飞龙 协议:CC BY-NC-SA 4.0 贡献指南 本项目需要校对,欢迎大家提交 Pull Request。 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并...

ApacheCN_飞龙
54分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部