18

Git在rebase时如何保留merge commit

 4 years ago
source link: https://yanhaijing.com/git/2020/09/23/git-rebase-merge/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

rebase是git中一个神奇的命令,可以让并行的提交,变得像线性开发的一样,最近发现rebase在遇到merge commit时可能和认知不太一样,本文通过一个例子来讲解下git rebase遇到merge commit时该如何应对

经典rebase

先来看下rebase的典型使用场景,假设我们有如下一个提交记录

543.png

当dev想同步上游(master)的修改时,有两种方案,一种是merge,一种是rebase

merge会创建一个commit节点,并且能够保留分支关系

$ git merge master
544.png

但是一般上游到下游的操作,是不需要保留分支关系的,rebase正是为了这个场景设计的,rebase会将dev上的节点,重新在master上重新创建一遍,看起来就是是在master分支最后开发的一样

$ git rebase master
545.png

至此就介绍完了rebase常规的使用场景

merge rebase

如果被rebase的分支上有merge commit,会发生什么呢?假设我们有如下的log tree(请原谅我懒得画图了^_^)

* 31ef4ec (HEAD -> master) e
| *   b554f2d (dev) Merge branch 'test' into dev
| |\  
| | * 853aaf6 (test) c
| * | 1af86fa d
| |/  
| * c1b49a5 b
|/  
* 763a350 a

master分支在dev分支后面又有了新的提交,dev分支合并了test分支,现在dev分支想同步master分支,继续使用rebase操作

* 8924fda (HEAD -> dev) c
* 06201b2 d
* e0c6b78 b
* 31ef4ec (master) e
| * 853aaf6 (test) c
| * c1b49a5 b
|/  
* 763a350 a

可以rebase将merge commit抛弃了,同时test分支上的提交c的内容被patch到了dev分支上

这可能和我的认知不太一样,我希望的效果是继续保留merge commit,rebase 为我们提供--rebase-merges参数来这个问题

$ git rebase --rebase-merges master

看下log tree,变基成功了,merge commit被保留了,但test分支也被变基了,test分支上的commit还是被复制了一份

*   88e6cf7 (HEAD -> dev) Merge branch 'test' into dev
|\  
| * e2fae10 c
* | 65869a2 d
|/  
* e61592d b
* 31ef4ec (master) e
| * 853aaf6 (test) c
| * c1b49a5 b
|/  
* 763a350 a

最后建议大家merge和rebase尽量使用一种即可,即rebase的代码,不要包含merge commit;使用了merge操作,就不要在rebase了

原文网址:http://yanhaijing.com/git/2020/09/23/git-rebase-merge/

微信公众号:颜海镜关注微信公众号 颜海镜微信支付二维码赞赏支持 微信扫一扫

Recommend

  • 15
    • lotabout.me 4 years ago
    • Cache

    Git: merge vs rebase

    Table of ContentsMerge 还是 Rebase,这是一个问题。网上有许多教程说明二者的区别,之前我写的一个 关于 Git 的 PPT 里也说过两者的区...

  • 19
    • reflectoring.io 4 years ago
    • Cache

    Git Rebase vs. Git Merge Explained

    In this article, we are going to discuss some very important commands from Git and how they make the life of developers easy - working individually or in a team. We will compare git rebase with...

  • 9

    Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...

  • 12
    • forrestsu.github.io 2 years ago
    • Cache

    git merge 和git rebase的区别

    git merge 和git rebase的区别 2016年9月11日 | 字数 863 |

  • 7

    如何 Rebase 第一個 Commit?-黑暗執行緒 Git 最大的好處是可以隨心所欲 Commit、切 Branch,想做就做,盡情開發,歷程再怎麼瑣碎噁心都無所謂,反正原始碼 Push 前都可用 Git Rebase 指令重新整理到美美的再公諸於世(延伸閱讀...

  • 6

    Git Merge vs. Rebase: Which to Use and When One of the most powerful features of Git is its ability to create...

  • 10

    When do you use Git rebase instead of Git merge? ...

  • 15

    Open Source (13 Part Series) When Merging a Pull Request on GitHub, you ma...

  • 6

    git rebase命令常常因为江湖上关于它是一种Git魔法命令的名声而导致Git新手对它敬而远之,但是事实上如果一个团队能够正确使用的话,它确实可以让生活变得更简单。在这篇文章中我们会比较git rebase和经常与之相提并论的git merge命令,并且在真实典型的Git工作流程...

  • 7
    • forrestsu.github.io 1 year ago
    • Cache

    git rebase 保留提交者原始日期?

    1 Preface 当使用 git cherry-pick A^..B 来 copy 一组 commits 时,会默认使用当前的系统时间; 我们想修改一组 commits 的 author和 email,但是要保留 commit-date 和

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK