Git rebase使用详解

Git rebase使用详解

大家好,又见面了,我是全栈君。

git rebase能够将分叉的分支重新合并,之前写过一篇文章介绍它的原理,下面主要介绍它的两个使用场景:

场景一:本地与远端同一分支提交历史不一致

方式一

多个人在同一个分支上协作时,出现冲突是很正常的,比如现在有一个项目由我和A一同开发。

我在修复了一个bug以后准备提交

HowiedeiMac:ganlin howie$ git add models/paper.go
HowiedeiMac:ganlin howie$ git commit -m 'fix a bug'
[master 8b76654] fix a bug
 1 file changed, 3 insertions(+), 3 deletions(-)

现在准备推送到远端

HowiedeiMac:ganlin howie$ git push origin master
To https://gitee.com/greenhn/ganlin.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/greenhn/ganlin.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

push失败了,说明A在我之前已经提交了,我本地master分支的提交历史已经落后远端了,需要先pull一下,与远端同步后才能push

HowiedeiMac:ganlin howie$ git pull
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 6), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From https://gitee.com/greenhn/ganlin
   a1bc60a..b91f711  master     -> origin/master
Merge made by the 'recursive' strategy.
 controllers/deal_local_data.go | 14 +++++++++++---
 controllers/rtu_interface.go   |  8 ++++----
 models/instrument_type.go      |  3 +++
 models/rtu_interface.go        |  3 +++
 4 files changed, 21 insertions(+), 7 deletions(-)


pull成功,现在使用git log看下一提交历史:

HowiedeiMac:ganlin howie$ git log --oneline --graph
*   f63ecbf (HEAD -> master) Merge branch 'master' of https://gitee.com/greenhn/ganlin
|\  
| * b91f711 (origin/master, origin/HEAD) 修正bug,优化内置通道配置
* | 8b76654 fix a bug
|/  
* a1bc60a 完善日报接口
* 9f73b5e 增加内置通道设置功能
* a0d464e ...

竟然分叉了!由于我本地master的提交历史和远端的master分支的提交历史不一致,所以git为我进行了自动合并,然后生成了一个新的提交历史(f63ecbf Merge branch 'master' of

对于部分强迫症来说这个不能接受的,不想看到分叉。

这个时候用git rebase就可以解决

HowiedeiMac:ganlin howie$ git rebase
First, rewinding head to replay your work on top of it...
Applying: fix a bug

现在再查看一下提交历史:

HowiedeiMac:ganlin howie$ git log --oneline --graph
* 2e2b995 (HEAD -> master) fix a bug
* b91f711 (origin/master, origin/HEAD) 修正bug,优化内置通道配置
* a1bc60a 完善日报接口
* 9f73b5e 增加内置通道设置功能
* a0d464e ...

完美解决,现在再push推送到远端:

HowiedeiMac:ganlin howie$ git push origin master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 394 bytes | 394.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Powered By Gitee.com
To https://gitee.com/greenhn/ganlin.git
   b91f711..2e2b995  master -> master

再次查看提交历史

HowiedeiMac:ganlin howie$ git lg --oneline --graph
* 2e2b995 (HEAD -> master, origin/master, origin/HEAD) fix a bug
* b91f711 修正bug,优化内置通道配置
* a1bc60a 完善日报接口
* 9f73b5e 增加内置通道设置功能
* a0d464e ...

现在远端master,远端head,本地master全部统一,问题解决。

方式二

直接执行:
git pull --rebase
效果与上面是一致的,也是最近才发现,推荐使用

场景二:不同分支之间的合并

由于老板突发奇想,要求开发一个新的功能。

先创建一个分支用于开发新功能:

git checkout -b feature

HowiedeiMac:hello howie$ git checkout -b feature
Switched to a new branch 'feature'
HowiedeiMac:hello howie$ git branch
* feature
  master

接下来修改newFunc.go,增加新的功能,并且保存提交

vim newFunc.go
git add newFunc.go
git commit -m 'add new func'

现在查看一下提交

HowiedeiMac:hello howie$ git log --oneline --graph
* 4f58ab8 (HEAD -> feature) add new func
* 94c134b (master) init base


HowiedeiMac:hello howie$ git branch
* feature
  master

现在新功能开发完毕,需要将它合并的主分支中。

先尝试通过merge合并:

首先切换到master分支

git checkout master

HowiedeiMac:hello howie$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

直接合并feature分支

git merge feature

HowiedeiMac:hello howie$ git merge feature
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
Automatic merge failed; fix conflicts and then commit the result.

竟然失败了,说明我两个分支之前的版本已经不同步了,需要手动合并冲突,再提交:

先查看冲突文件:git status

HowiedeiMac:hello howie$ git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
  (use "git push" to publish your local commits)


You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)


Unmerged paths:
  (use "git add <file>..." to mark resolution)


        both modified:   newFunc.go

Git rebase使用详解

打开文件,进行修改

原文件:

func NewFunc() {
<<<<<<< HEAD
=======
    fmt.Println("add new func")
>>>>>>> feature
}

修改后:

func NewFunc() {
    fmt.Println("add new func")
}

现在通过add添加,然后commit提交

HowiedeiMac:hello howie$ git add newFunc.go


HowiedeiMac:hello howie$ git commit -m 'merge master and feature'
[master 562ec58] merge master and feature

现在在查看一下分支提交历史:

HowiedeiMac:hello howie$ git log --oneline --graph
*   562ec58 (HEAD -> master) merge master and feature
|\  
| * 4f58ab8 (feature) add new func
* | 0e80f97 do something
|/  
* 94c134b init base

虽然合并成功,但是Master已经保存了合并历史,出现开叉了!对于强迫症患者来说肯定是不能接受的。

通过rebase合并分支:

现在将版本退回到合并前,也就是回退一个版本

git reset --hard head^

HowiedeiMac:hello howie$ git reset --hard head^
HEAD is now at 0e80f97 do something


HowiedeiMac:hello howie$ git log --oneline --graph
* 0e80f97 (HEAD -> master) do something
* 94c134b init base

退回去了,现在是位于master分支的init base提交这里。

先切换回feature分支:

HowiedeiMac:hello howie$ git checkout feature
Switched to branch 'feature'

在feature分支上执行: git rebase master

这句命令的意识是:以master为基础,将feature分支上的修改增加到master分支上,并生成新的版本。

HowiedeiMac:hello howie$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add new func
Using index info to reconstruct a base tree...
M       newFunc.go
Falling back to patching base and 3-way merge...
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
error: Failed to merge in the changes.
Patch failed at 0001 add new func
hint: Use 'git am --show-current-patch' to see the failed patch


Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".

失败了,原因很简单,两个分支修改个同一个文件,产生了冲突。所以先需要解决冲突:

打开冲突的文件,解决冲突

原文件:

func NewFunc() {
<<<<<<< HEAD
=======
    fmt.Println("add new func")
>>>>>>> add new func
}

修改后:

func NewFunc() {
    fmt.Println("add new func")
}

现在通过add添加

HowiedeiMac:hello howie$ git add newFunc.go

现在是重点,之前的rebase其实只是完成了一半,由于出现冲突而终止,现在冲突解决,可以通过git rebase —continue继续完成之前的rebase操作。

HowiedeiMac:hello howie$ git rebase --continue
Applying: add new func

rebase完成,再查看一下提交历史:

HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> feature) add new func
* 0e80f97 (master) do something
* 94c134b init base

提交记录已经是一条完美的直线。现在切换到主分支master,将feather分支上的提交合并过来。

git checkout master
git merge feature
HowiedeiMac:hello howie$ git checkout master


Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.
  (use "git push" to publish your local commits)


HowiedeiMac:hello howie$ git merge feature
Updating 0e80f97..b2593e6
Fast-forward
 newFunc.go | 1 +
 1 file changed, 1 insertion(+)

再次查看一下提交历史:

HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> master, feature) add new func
* 0e80f97 do something
* 94c134b init base

问题解决,master上也是一条直线了。

最后收个尾,删除掉feature分支:

HowiedeiMac:hello howie$ git branch -d feature
Deleted branch feature (was b2593e6).

Git rebase使用详解

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111605.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Go 1 – 概览/安装

    Go 1 – 概览/安装安装包下载地址一安装Golang的SDK二配置环境变量三使用命令行调试参考文章地址网上有很多安装方法,比如通过homebrew来安装。这里是直接下载的安装包。安装包下载地址地址:https://golang.org/dl/一、安装Golang的SDK双击.tar文件,就会自动解压成名字为“go”的文件夹;拖拽到你的用户名下,记住路径;我的路径为:/Users/MelissaShu

    2022年10月11日
    1
  • 什么叫pure function(纯函数)[通俗易懂]

    什么叫pure function(纯函数)[通俗易懂]在Knockout中,用到了pureComputer(),其原理来自于纯函数(purefunction)。那么,什么叫纯函数呢?纯函数(来自:http://en.wikipedia.org/wiki/Pure_function)     在计算机编程中,假如满足下面这两个句子的约束,一个函数可能被描述为一个纯函数:给出同样的参数值,该函数总是求出同样的结果。该函数结

    2025年6月9日
    0
  • 15种手机游戏引擎和开发工具介绍

    15种手机游戏引擎和开发工具介绍工欲善其事,必先利其器。对移动游戏开发者来说,高效实用的开发工具必不可少。近日,英国著名产业杂志《Develop》刊出了一篇文章,作者艾伦·李在文中推荐了15种移动游戏开发工具,从游戏引擎,到音效制作、推广等工具都有涉及。以下为原文主要内容编译。引擎和移动开发工具包Marmalade简介:Marmalade被很多人认为是跨平台制作C++游戏的最佳平台。通过MarmaladeSDK,开发者可以在单一的Marmalade项目文件夹中打开Xcode或VisualStudio,将

    2022年5月22日
    134
  • mysql修改表名

    mysql修改表名ALTER TABLE table_nameRENAMETOnew_table_name

    2022年6月1日
    25
  • tidb数据库隔离级别剖析

    tidb数据库隔离级别剖析本文章来源于:https://github.com/Zeb-D/my-review,请star强力支持,你的支持,就是我的动力。[TOC]前言在线应用业务中,数据库是一个非常重要的组成部分,特别是现在的微服务架构为了获得水平扩展能力,我们倾向于将状态都存储在数据库中,这要求数据库能够正确、高性能处理请求,但这是一个几乎不可能达到的要求,所以数据库的设计者们定义了隔离级别这一个概念,在高…

    2022年5月25日
    38
  • ASP脚本_笛子入门基础教程手指训练

    ASP脚本_笛子入门基础教程手指训练通过前两篇的学习,相信各位已经对ASP的动态网站设计有了一个基本的概念和整体的印象。从本篇开始作者将从脚本语言的使用着手,由浅入深地带领大家探索ASP动态网站设计的真正奥秘。本文第二篇刊登后

    2022年8月1日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号