Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁。

2024-01-21 18:08

本文主要是介绍Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.如下所示,自上次push以后,我在本地有三次零散的commit,但是还没有push,因为不想提交到server上显得太过凌乱,所以要合并以下再push,这就要用到’git rebase’:
peng@host:~/gitTest$ git log
commit a92aea92169587086679cd13af4cf2dc335ceca5 (HEAD -> master)
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 21:43:34 2020 +08003st commit
commit 0c971470adcbc429c7b2709af18318f2d523439a
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 21:43:13 2020 +08002st commit
commit 3db8de23e1253c48caadf9fb0393659dadf0fdf9
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 21:42:51 2020 +08001st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 20:40:00 2020 +0800cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <31085468+songpeng22@users.noreply.github.com>
Date:   Wed Jul 22 19:41:08 2020 +0800Initial commit
2.git rebase的使用:

合并最近三次到提交:

git rebase -i HEAD~3

于是出现了(这里是自动用vim打开):

  1 pick 3db8de2 1st commit2 pick 0c97147 2st commit3 pick a92aea9 3st commit4 5 # Rebase 5bb1c9b..a92aea9 onto 5bb1c9b (3 commands)6 #7 # Commands:8 # p, pick <commit> = use commit9 # r, reword <commit> = use commit, but edit the commit message10 # e, edit <commit> = use commit, but stop for amending11 # s, squash <commit> = use commit, but meld into previous commit12 # f, fixup <commit> = like "squash", but discard this commit's log message13 # x, exec <command> = run command (the rest of the line) using shell14 # b, break = stop here (continue rebase later with 'git rebase --continue')15 # d, drop <commit> = remove commit16 # l, label <label> = label current HEAD with a name17 # t, reset <label> = reset HEAD to a label18 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]19 # .       create a  merge commit using the original merge commit's20 # .       message (or the oneline, if no original merge commit was21 # .       specified). Use -c <commit> to reword the commit message.22 #23 # These lines can be re-ordered; they are executed from top to bottom.24 #25 # If you remove a line here THAT COMMIT WILL BE LOST.26 #27 # However, if you remove everything, the rebase will be aborted.28 #29 # Note that empty commits are commented out
3. 合并方式

我们想把三次commit 合并成一个,这就要用到上面文本里到一些操作命令。这里选squash( use commit , but meld into previous commit) ,将后面的塞入之前的commit
将上面前三行的内容修改为

  1 p 3db8de2 1st commit2 s 0c97147 2st commit3 s a92aea9 3st commit
略...................................
4.修改commit日志

上面修改后,输入wq保存后,跳出修改日志的内容:

  1 # This is a combination of 3 commits.2 # This is the 1st commit message:4 1st commit6 # This is the commit message #2:8 2st commit10 # This is the commit message #3:12 3st commit14 # Please enter the commit message for your changes. Lines starting15 # with '#' will be ignored, and an empty message aborts the commit.16 #17 # Date:      Wed Jul 22 21:42:51 2020 +080018 #19 # interactive rebase in progress; onto 5bb1c9b20 # Last commands done (3 commands done):21 #    squash 0c97147 2st commit22 #    squash a92aea9 3st commit23 # No commands remaining.24 # You are currently rebasing branch 'master' on '5bb1c9b'.26 # Changes to be committed:27 #   modified:   README.md28 #

我将之修改为

  1 # This is a combination of 3 commits.2 combined log:3 1st commit4 2st commit5 3st commit7 # Please enter the commit message for your changes. Lines starting8 # with '#' will be ignored, and an empty message aborts the commit.9 #10 # Date:      Wed Jul 22 21:42:51 2020 +080011 #12 # interactive rebase in progress; onto 5bb1c9b13 # Last commands done (3 commands done):14 #    squash 0c97147 2st commit15 #    squash a92aea9 3st commit16 # No commands remaining.17 # You are currently rebasing branch 'master' on '5bb1c9b'.18 #19 # Changes to be committed:20 #   modified:   README.md
5.结果

输入git log,可以看到日志合并结果如下:

peng@host:~/gitTest$ git log
commit ee32c58cc8975b40cb7f86abe7c78206b2590256 (HEAD -> master)
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 21:42:51 2020 +0800combined log:1st commit2st commit3st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 20:40:00 2020 +0800cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <31085468+songpeng22@users.noreply.github.com>
Date:   Wed Jul 22 19:41:08 2020 +0800Initial commit
6.push
sudo git push -f origin <branch>
6-1. rebase + force push 可能会导致的混乱

如果你的同事在你rebase之前,下载了你想要rebase的commit ,那么你的rebase + force push 可能会带来一些问题。
解决思路1:
可能需要 rebase 的 commit,先不要上传,rebase之后再统一上传。
解决思路2:
各人各用一个branch ,互相不干扰。你上传的时候只 force push 到你自己的branch 上面,如下:
sudo git push -f origin <branch>

6-2 错误做法 - 如果直接 git push ,会被reject , 有类似提示:
error: failed to push some refs to 'https://name@github.com/***/***.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因是rebase之后,remote repository 的 commit 和 local repository的commit 就不一样了,让你merge。
比较奇怪的是 为什么说 current branch 是 behind remote repository。
我的理解,也许以服务器为起源/origin的话,只要服务器有你没有的commit , 你就算behind了。

参考文献:
彻底搞懂git:
http://jartto.wang/2018/12/11/git-rebase/
Git commits历史是如何做到如此清爽的:
https://www.zhihu.com/question/61283395
在这里插入图片描述

这篇关于Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/630397

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Redis 多规则限流和防重复提交方案实现小结

《Redis多规则限流和防重复提交方案实现小结》本文主要介绍了Redis多规则限流和防重复提交方案实现小结,包括使用String结构和Zset结构来记录用户IP的访问次数,具有一定的参考价值,感兴趣... 目录一:使用 String 结构记录固定时间段内某用户 IP 访问某接口的次数二:使用 Zset 进行

Git提交代码详细流程及问题总结

《Git提交代码详细流程及问题总结》:本文主要介绍Git的三大分区,分别是工作区、暂存区和版本库,并详细描述了提交、推送、拉取代码和合并分支的流程,文中通过代码介绍的非常详解,需要的朋友可以参考下... 目录1.git 三大分区2.Git提交、推送、拉取代码、合并分支详细流程3.问题总结4.git push

mysqld_multi在Linux服务器上运行多个MySQL实例

《mysqld_multi在Linux服务器上运行多个MySQL实例》在Linux系统上使用mysqld_multi来启动和管理多个MySQL实例是一种常见的做法,这种方式允许你在同一台机器上运行多个... 目录1. 安装mysql2. 配置文件示例配置文件3. 创建数据目录4. 启动和管理实例启动所有实例

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

Git中恢复已删除分支的几种方法

《Git中恢复已删除分支的几种方法》:本文主要介绍在Git中恢复已删除分支的几种方法,包括查找提交记录、恢复分支、推送恢复的分支等步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录1. 恢复本地删除的分支场景方法2. 恢复远程删除的分支场景方法3. 恢复未推送的本地删除分支场景方法4. 恢复

使用SQL语言查询多个Excel表格的操作方法

《使用SQL语言查询多个Excel表格的操作方法》本文介绍了如何使用SQL语言查询多个Excel表格,通过将所有Excel表格放入一个.xlsx文件中,并使用pandas和pandasql库进行读取和... 目录如何用SQL语言查询多个Excel表格如何使用sql查询excel内容1. 简介2. 实现思路3

基于C#实现PDF文件合并工具

《基于C#实现PDF文件合并工具》这篇文章主要为大家详细介绍了如何基于C#实现一个简单的PDF文件合并工具,文中的示例代码简洁易懂,有需要的小伙伴可以跟随小编一起学习一下... 界面主要用于发票PDF文件的合并。经常出差要报销的很有用。代码using System;using System.Col