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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • FlashFXP最新密钥-FlashFXP永久授权注册码分享

    FlashFXP最新密钥-FlashFXP永久授权注册码分享FlashFXP是一个功能强大的FXP/FTP软件,融合了一些其他优秀FTP软件的优点,如像CuteFTP一样可以比较文件夹,支持彩色文字显示;像BpFTP支持多文件夹选择文件,能够缓存

    2022年7月2日
  • Win10加Ubuntu20.04双系统安装教程

    Win10加Ubuntu20.04双系统安装教程安装步骤软件准备制作U盘启动盘windows下硬盘分区进入BIOS系统进行硬盘安装软件准备本文编辑日期是2020.8.4,此时最新版的Ubuntu系统版本为20.04。下载Ubuntu系统:Ubuntu下载中文站下载U盘启动盘制作软件:百度网盘提取码:uhrmEasyBCD(开机启动选择系统):百度网盘提取码:mxg2制作U盘启动盘准备一个4G以上的U盘做启动盘即可,制作系统盘时会将u盘格式化,注意将u盘内的文件做个备份。Ubuntu系统下载好后是一个.iso文件,如图打开UI

    2022年7月24日
  • VMI管理常见的业务场景(一)[通俗易懂]

    VMI管理常见的业务场景(一)[通俗易懂]VMI管理常见的业务场景(一)什么是VMI管理?和传统库存管理的差别VMI是供应商管理库存(VendorManagedInventory)的简称,为了界定清楚这个新的库存管理思想,需要弄明白它和传统库存管理的差别。传统库存管理是指对物料的收发存的业务管理,是各节点企业独立管理自有库存,最明显的缺点是容易存在库存管理的牛鞭效应、上下游企业利益对抗、合作与沟通困难等问题。VMI库存管理系统能够突破传统的条块分割的库存管理模式,VMI是一种在用户和供应商之间…

    2022年5月26日
  • 对成绩进行排序c语言_c语言对学生成绩进行排序

    对成绩进行排序c语言_c语言对学生成绩进行排序解题思路:注意事项:注意姓名字符串的长度要大于8,因为这个调了很多次参考代码:#include#include#includeusingnamespacestd;structstudent{intnumber;charname[9];intscore;}buf[100000];boolcmp1(studenta,studentb){returna.number}boolcm…

    2022年9月25日
  • JAVA运用dos命令强制删除文件夹

    JAVA运用dos命令强制删除文件夹在对文件夹进行操作时,如果用file.deleate()方法,有时候恐怕不凑效.用了第三方的FileUtils的forceDeleteFile()还是不凑效,所以,自己就用java写一个基于dos命令的方法来实现强制删除文件夹.这并没有什么难的,只要知道dos命令,在java文件中调用runtime就好办了.在这里为写了三个方法:删除某个文件目录;删除某个文件(不是文件夹)和清空某个文件夹下

    2022年6月8日
  • 在Java中,关于a=a+b与a+=b的区别「建议收藏」

    在Java中,关于a=a+b与a+=b的区别「建议收藏」在Java中,关于a=a+b与a+=b的区别

    2022年4月23日

发表回复

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

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