git合并历史提交
背景
以前一直觉得只要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的简单指令:
参阅vim的简单使用
准备一个测试repo
git init test-rebase
cd test-rebase
提交一个文件多次:
vim test.txt
//输入第一次提交。
git add test.txt
git commit -m "1"
vim test.txt
//输入第2次提交。
git add test.txt
git commit -m "2"
vim test.txt
//输入第3次提交。
git add test.txt
git commit -m "3"
查看log:
git log
//////
commit 0353373749d72e53a34c7bdda86d77d7bb3ca6fe
Author: ryan <v-rmiao@expedia.com>
Date: Wed Jul 19 13:23:18 2017 +0800
3
commit acf6d24adc2097fda82d29064e8edfef6355d01d
Author: ryan <v-rmiao@expedia.com>
Date: Wed Jul 19 13:20:37 2017 +0800
2
commit 2169bc5e20386951b19aff32143e74f2da683df2
Author: ryan <v-rmiao@expedia.com>
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 2
pick 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 2
squash 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 8f54e6b5643ff26ac967a9e6e6cded68a6c50906
Author: ryan <v-rmiao@expedia.com>
Date: Wed Jul 19 13:20:37 2017 +0800
这是合并后的message,以下是之前合并的历史
2
3
commit 2169bc5e20386951b19aff32143e74f2da683df2
Author: ryan <v-rmiao@expedia.com>
Date: Wed Jul 19 13:19:42 2017 +0800
1
参考
https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E5%86%99%E5%8E%86%E5%8F%B2
- kmp模版
- 触发器在渗透中的利用
- Codeforces 626B Cards(模拟+规律)
- Android 5.0屏幕录制漏洞(CVE-2015-3878)威胁预警
- Codeforces 626A Robot Sequence(模拟)
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】
- [接口测试 - 基础篇] 01 你应该了解的协议基础
- Uva - 12050 Palindrome Numbers【数论】
- [接口测试 - http.client篇] 14 源码初探及其工作机制分析
- 51Nod 1277 字符串中的最大值(KMP,裸题)
- Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】
- 07.移动先行之谁主沉浮----控件之轮流轰炸——布局类控件
- BZOJ 1411&&Vijos 1544 : [ZJOI2009]硬币游戏【递推,快速幂】
- UVa 10341 - Solve It【经典二分,单调性求解】
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [周末往期回顾]使用BBED定位数据位置
- JeecgCloud版,新建项目。
- MySQL忘记root密码,错误号码1045解决办法
- java_流程控制语句、权限修饰符
- [Oracle 故障处理]记一次RMAN备份警告的处理过程
- [Oracle 日常管理]使用oradebug捕获SQL语句
- [Oracle 日常管理]ERRORSTACK使用介绍
- Oracle参数解析(nls_numeric_characters)
- 在Linux系统中安装Tomcat
- java_缓冲流、转换流、序列化流
- Kali内网使用正向shell入侵
- KALI拿到shell后提权操作
- KALI 内网渗透记录
- CentOS7部署Grafana
- CentOS7网卡配置文件详解