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-工具-重写历史
git合并历史提交的更多相关文章
- git 修改历史提交信息
当你不小心,写错了提交的注视/信息,该如何处理呢.理论上,SCM是不应该修改历史的信息的,提交的注释也是. 不过在git中,其commit提供了一个--amend参数,可以修改最后一次提交的信息. ...
- Git 修改历史提交信息 commit message
修改最近一条提交的消息 git commit --amend 进入vim模式 按字母 o 或者 insert键 开始修改内容 按 esc 推出编辑,最常用的是输入":q"直接退出, ...
- git 合并某个提交commit到指定的分支上
https://blog.csdn.net/anhenzhufeng/article/details/77962943 git checkout master git cherry-pick 62ec ...
- 4.Git基础-查看提交历史
1.查看提交历史 -- git log 使用 git log 可以查看到所有的提交(commit)历史. 1. $ git log 列出所有commit,最新的commit在最上面.会显示每个提交 ...
- Git 基础 - 查看提交历史
查看提交历史 在提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,可以使用 git log 命令查看. 接下来的例子会用我专门用于演示的 simplegit 项目,运行下面的命令获取该项目源 ...
- git杂记-查看历史提交
普通查看:git log.输入q退出比较. $ git log commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon ...
- git log 查看提交历史
文章出处http://blog.csdn.net/wh_19910525/article/details/7468549 git log 查看 提交历史 在提交了若干更新之后,又或者克隆了某个项目 ...
- git学习——查看提交历史
git log可以查看提交历史: 用-p选项展开显示每次提交的内容差异,用-2则仅显示最近两次的更新:git log -p -2 在-p选项后面使用--word-diff选项进行单词层面的对比.这其中 ...
- Git使用四:查看工作状态和历史提交
查看当前的工作状态:git status On branch master:现在位于master分支里面nothing to commit, working tree clean:没有需要提交的文件, ...
随机推荐
- 全景技术大揭秘,市场核心早洞悉——VR全景加盟
未来已来,未来已见.2017是3D全景创业的天时,全景行业逐步走向成熟.全景智慧城市专注vr全景6年,技术国内遥遥领先.全景智慧城市市场总监常诚,透漏3D全景技术和市场的核心. 拍摄全景必备的设备:单 ...
- 提高code效率
分享下个人编码挫折,关于提高编码效率.代码规范.清晰的代码模块顺序.及时总结代码(提取出可复用的)以及清晰的注释,这是我感觉有必要的,因为工作到后期,代码量都非常的大.就是上个周5那天的整体工作效率都 ...
- WEB前端:浏览器(IE+Chrome+Firefox)常见兼容问题处理--02
兼容问题目录 8.IE6不支持固定定位 9.IE6下前面元素浮动,后面元素不浮动后他们之间会有间隙 10.IE6下双边距问题 11.IE67下父级有边框,子级有margin的话会不起作用 12.IE6 ...
- mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记
前言:用mybatis也好几年了,mybatis在批量的增删操作也写起来也是比较简单的,只有批量更新这一块是特别坑,特此记录. 注:本文主要用来记录oracle和mysql数据库在使用mybatis的 ...
- 后台开发之IO缓冲区管理
Linux系统IO中write原型为 ssize_t write(int filedes, const void * buff, size_t nbytes) ; 当调用write写数据的时候,调 ...
- Vue爬坑之vuex初识
在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 http://www.cnblogs.com/wisewrong/p/62660 ...
- MySQL分页优化中的“INNER JOIN方式优化分页算法”到底在什么情况下会生效?
本文出处:http://www.cnblogs.com/wy123/p/7003157.html 最近无意间看到一个MySQL分页优化的测试案例,并没有非常具体地说明测试场景的情况下,给出了一种经典的 ...
- Sql Server 事物
事物的定义: 数据库的事物是原子性的,原子性的概念是指把一些事情当做一个单元来看待. 从数据库的角度看,它是指全部执行或者全部不执行的一条或者多条语句的最小组合:为了理解事务的概念,需要能够定义非常明 ...
- maven项目转成web项目没有生成WebContent目录
有时候建立maven项目转成web项目没有生成WebContent目录,此时把Dynamic web module 去掉勾选,然后ok,再点开项目的properties,再选中Dynamic web ...
- python网络爬虫之初始网络爬虫
第一次接触到python是一个很偶然的因素,由于经常在网上看连载小说,很多小说都是上几百的连载.因此想到能不能自己做一个工具自动下载这些小说,然后copy到电脑或者手机上,这样在没有网络或者网络信号不 ...