博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git合并历史提交
阅读量:7050 次
发布时间:2019-06-28

本文共 3700 字,大约阅读时间需要 12 分钟。

背景

以前一直觉得只要pull和push就够了,但合作中总会遇到各种非理想的情况。这时候才发现git其他命令的作用。

现在的情况是,repo是一个远程team维护的,我们需要增加新feature,那么就是一个feature分支了。由于开发中各种修改,本feature分支多次commit。最后,交给远程team review的时候,人家看着乱七八糟的修改历史很蛋疼:很难看懂各种增量修改。其实,对人家来说,我们的改动应该就是增加或者删除。给他们看开发过程的增量反而太乱。于是,人家要求我们将feature分支的提交合并,这样看起来清爽。

一些简单的命令准备

合并分支的命令是rebase,除此之外,其他的一些命令也应该知晓。

查看commit历史

git log

查看当前状态

git status

添加所有文件

git add .

提交修改

git commit -m "本次提交添加了xxxx"

vim的简单指令:

参阅

准备一个测试repo

git init test-rebasecd test-rebase

提交一个文件多次:

vim test.txt//输入第一次提交。   git add test.txtgit commit -m "1"vim test.txt//输入第2次提交。   git add test.txtgit commit -m "2"vim test.txt//输入第3次提交。   git add test.txtgit commit -m "3"

查看log:

git log//commit 0353373749d72e53a34c7bdda86d77d7bb3ca6feAuthor: ryan 
Date: Wed Jul 19 13:23:18 2017 +0800 3commit acf6d24adc2097fda82d29064e8edfef6355d01dAuthor: ryan
Date: Wed Jul 19 13:20:37 2017 +0800 2commit 2169bc5e20386951b19aff32143e74f2da683df2Author: ryan
Date: Wed Jul 19 13:19:42 2017 +0800 1

可以看到有三次提交了。现在我们想要把第2次和第3次提交的内容合并成一次提交。

开始rebase

1. 复制合并前的一次提交的hash

这里就是第一次提交的hash。即2169bc5e2

2. git rebase -i xxx

git rebase -i 2169bc5e2

进入历史提交的编辑页面,此时编辑方式为vim。

pick acf6d24 2pick 0353373 3# Rebase 2169bc5..0353373 onto 2169bc5 (2 commands)## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Note that empty commits are commented out

可以看到第2次和第3次的提交消息,并且是从old->new来排序的。我们需要把第3次提交合并到第2次上。使用squash.

squash

修改第三次提交为squash,意思是和前一次(第二次)提交合并。

键盘按键j移动到第二行,然后按a开始编辑,删除pick,插入squash
如下:

pick acf6d24 2squash  0353373 3# Rebase 2169bc5..0353373 onto 2169bc5 (2 commands)## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Note that empty commits are commented out

然后,按esc退出编辑,再按:,输入wq保存。

这时候会进入第二个vim页面,这里让我们再次修改commit message的。就是合并后的message。

# This is a combination of 2 commits.这是合并后的message,以下是之前合并的历史# This is the 1st commit message:2# This is the commit message #2:3# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date:      Wed Jul 19 13:20:37 2017 +0800## interactive rebase in progress; onto 2169bc5# Last commands done (2 commands done):#    pick acf6d24 2#    squash 0353373 3# No commands remaining.# You are currently editing a commit while rebasing branch 'master' on '2169bc5'.## Changes to be committed:

还是和刚才一样,按o插入下一行,输入这次合并的message。然后按esc,按:, 输入wq保存并退出。

完事,再次查看log

git log

内容如下:

commit 8f54e6b5643ff26ac967a9e6e6cded68a6c50906Author: ryan 
Date: Wed Jul 19 13:20:37 2017 +0800 这是合并后的message,以下是之前合并的历史 2 3commit 2169bc5e20386951b19aff32143e74f2da683df2Author: ryan
Date: Wed Jul 19 13:19:42 2017 +0800 1

68747470733a2f2f7777772e61746c6173736961

本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/git-rebase.html,如需转载请自行联系原作者

你可能感兴趣的文章
Stream API(三)--原始类型流特化
查看>>
使用webiopi控制树莓派的GPIO引脚电平(WEB在线管理)
查看>>
js中call与apply
查看>>
隐式转换
查看>>
Java 8 - 20 Examples of Date and Time API Read mo
查看>>
JDK8新特性-方法引用
查看>>
(转)直接拿来用!最火的iOS开源项目(二)
查看>>
MikroTik RouterOS ™ V2.6 基本安装
查看>>
有状态、无状态(Stateful and Stateless)
查看>>
XA式、非XA式Spring分布式事务的实现
查看>>
jquery 中的isArray方法分析
查看>>
C语言(3)指针
查看>>
mysql 复杂的查询语句,工作中用到的记录下
查看>>
Spring-Rabbit官方示例
查看>>
Jetson TX2 安装tensorflow
查看>>
鸟哥Linux私房菜基础学习篇 第一部分:Linux的规划与安装_Linux是什么
查看>>
spring+hibernate xml和注解方式配置事务
查看>>
CSS学习
查看>>
基于MOS9.0的OpenStack私有云部署
查看>>
连接MyBatis内部SqlSession与业务接口的代理类MapperProxy
查看>>