一图弄懂Git rebase

本文深入探讨了Git中两种核心分支合并策略:rebase与merge。通过实例讲解了rebase如何将实验分支的更改重新应用到主分支,以及它与merge在提交历史呈现上的差异。强调了rebase在本地开发中整理提交历史的重要性,以及不应对其已推送至远程仓库的内容进行rebase的原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种合并分支的方式:merge,rebase

With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch

rebase是如何合并分支的

如下图:采用rebase方式将experiment分支上的内容合并到master分支上
在这里插入图片描述

git rebase master这个命令幕后都做了什么操作:

  1. experiment分支的修改暂存起来
  2. experiment分支reset到master分支的状态
  3. 在重置后的experiment分支上replay刚刚暂存起来的修改

最后:切换到master分支,进行简单的fast-forward合并即可

为什么要用rebase(和merge的区别)

如上例中所示,rebase的提交历史更干净线性

何时(不)使用rebase

In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere.

  • 在本地开发时可以使用,让我们的提交历史更干净易读

  • 不要使用rebase合并已经push到远程仓库的内容

更多讨论可访问github:
https://github.com/pluscai/use-git/issues/18

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值