合并commit的做法一般用在pull request的时候,把开发同一功能时的所有琐碎的commit合并到一个(假装自己的代码是高质量代码(手动滑稽))。主要使用的命令是git rebase 或者git reset,这两个命令的区别可以看这里,一般推荐用git rebase,因为commit的历史能保存下来。

1. git rebase

首先用git log查看commit历史确定需要操作的commit,比如:

commit 0bca654809387dc226cfc4ff872f65601809f485
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date: Wed Aug 16 13:13:12 2017 +0800 fix typo commit b5bf9998e40dc165d7e3055bae47172de11225d4
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date: Wed Aug 16 10:02:30 2017 +0800 add random vertical flip during image preprocessing commit 70a4958184106b9ce848da34be34bdf0dbf36450
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date: Wed Aug 16 09:58:55 2017 +0800 step by step running commit d68608c2dbd41a1b89363f4b743bc5464b6749be
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date: Mon Aug 14 15:52:47 2017 +0800 modify description

如果需要操作最近4个commit:

git rebase -i HEAD~4   # 操作对象为从倒数第4个commit开始到当前的commit
# git rebase -i d68608c2dbd41a1b89363f4b743bc5464b6749be # 倒数第4个commit 的id

然后会跳出编辑页面像这样:

pick d68608c modify description
pick 70a4958 step by step running
pick b5bf999 add random vertical flip during image preprocessing
pick 0bca654 fix typo # Rebase 529bf46..0bca654 onto 529bf46 (4 command(s))
#
# 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

把需要保留的commit之前保留pick,需要合并的前面改成squash,编辑保存退出以后输入新的commit message,最后保存退出。

# This is a combination of 6 commits.
add random vertical flip for image preprocessing # 合并后的commit
# The first commit's message is:
test
# This is the 2nd commit message: object detection api usage step # This is the 3rd commit message: modify description # This is the 4th commit message: step by step running # This is the 5th commit message: add random vertical flip during image preprocessing # This is the 6th commit message: fix typo

这个时候再git log的日志如下:

commit 7f774d88eb6884c97c8b3c05a9268afa581d5b57
Author: Unknown <fanzongshaoxing@gmail.com>
Date: Mon Aug 14 15:45:29 2017 +0800 add random vertical flip for image preprocessing
test object detection api usage step modify description step by step running add random vertical flip during image preprocessing fix typo

如果修改了一半不想合并了就用git rebase --abort删除。

git reset

git reset --soft "HEAD~4"
git commit --amend

或者

git reset --hard HEAD~4  # 将branch状态切到到4个commit之前
git merge --squash HEAD@{1} # 将当前commit(此时为倒数第四个)之后的commit都合并
git commit # commit squashed changes

参考

https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git

https://stackoverflow.com/questions/2563632/how-can-i-merge-two-commits-into-one

http://zerodie.github.io/blog/2012/01/19/git-rebase-i/

Git合并最近的commit的更多相关文章

  1. (转)git合并多个commit

    原文地址:http://platinhom.github.io/2016/01/02/git-combine_commit/ 有时commit多了看着会不爽.所以想合并掉一些commit. 这里是最简 ...

  2. Git合并多个Commit

    当前有四个commit,现在要将四个commit合并为一个,可以使用git rebase -i HEAD~{这里是要合并的commit数量} 如 git rebase -i HEAD~4 ,即为合并最 ...

  3. Git合并一次commit到指定分支

    1 在当前分支,查看要合并的分支版本号 git log 需要合并的commit版本号 16b7df3aa1e64e00554a8a3c871e59db8cd87b16 2 切换到 指定分支 git c ...

  4. git 合并多个commit

    1,查看提交历史,git log 首先你要知道自己想合并的是哪几个提交,可以使用git log命令来查看提交历史,假如最近4条历史如下: commit 3ca6ec340edc66df13423f36 ...

  5. git 合并某个提交commit到指定的分支上

    https://blog.csdn.net/anhenzhufeng/article/details/77962943 git checkout master git cherry-pick 62ec ...

  6. IDEA git 合并多个commit

    当前三个commit,demo1,demo2,demo3 选择demo1右键 选择action 跟着指示操作,最后合并 时间线: Log 框时间线:是从上到下,越来越早. 弹出框时间线:是从上到下,越 ...

  7. Git自动化合并多个Commit

    目录 git rebase逻辑 git editor的修改 处理git-rebase-todo文件 Python实现 当我们有多个commit或者从开源处拿到多个commit时,想合成一个commit ...

  8. git合并分支上指定的commit

    merge 能够胜任平常大部分的合并需求.但也会遇到某些特殊的情况,例如正在开发一个新的功能,线上说有一个紧急的bug要修复.bug修好了但并不像把仍在开发的新功能代码也提交到线上去.这时候也许想要一 ...

  9. git将多个commit合并成一个

    1. 查看提交历史(最近10个) git log - 2. 回到前面第十个commit,且将后面九个commit提交的内容状态改为未提交 git reset commitID(第十个commit的ID ...

随机推荐

  1. 95. 不同的二叉搜索树 II

    95. 不同的二叉搜索树 II 题意 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 解题思路 这道题目是基于不同的二叉搜索树进行改进的: 对于连续整数序列[left, ri ...

  2. unity3d脚本语言中的引用类型

    在之前的文文里有说到,值类型和引用类型,那么这会就单独说下引用类型: Unity3D中的C#语言提供了专门的类型来为开发者提供使用C#开发游戏的便利条件: 在该引擎中,使用UnityEngine命名空 ...

  3. Android中AES256加密的实现

    AES加密是我们在工作中常用到一种加密方式,并且在java中也已经实现好了其相应的接口. 但是Java自带的JDK默认最多实现128位及其以下的加密.如果使用java自带的api实现aes256将会报 ...

  4. html5调用手机陀螺仪实现方向辨识

    获取移动设备的陀螺仪,需要知道陀螺仪包含什么. 我们可以让document监听deviceorientation 来获取相关的数据,里面包括3个值 alpha.beta和gamma. 这三个值分别代表 ...

  5. 转: chrome64打开弹出窗flash的办法

    https://jingyan.baidu.com/article/380abd0a38f0411d90192c2e.html

  6. Linux 10字符串命令病毒的处理记录

    刚上线的测试服务器不停的向外发包,且CPU持续100%,远程登录后查看发现有一长度为10的随机字符串进程,kill掉,会重新生成另外长度为10的字符串进程.删除文件也会重复生成,非常痛苦.查阅cron ...

  7. 微软BI 之SSRS 系列 - 基于时间段参数的 MDX 查询以及时间日历 Date Picker 的时间类型参数化

    今天在天善问答里看到一个问题,如果我没有理解错的话,它应该是指比如在一个报表中选取一个时间段,然后求出这个时间段的某个 Measure 的 SUM 和.并且同时求出这两个时间点对应的上一年的时间点之间 ...

  8. LeetCode 90:Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. 启动apache (OS 10022)提供了一个无效的參数。解决方式

    今天 apache 突然启动不起来了,查看了一下错误日志发现了例如以下错误: [Tue Mar 17 11:27:32 2015] [crit] Parent: child process exite ...

  10. 利用useragent判断移动设备

    用 User Agent 判断移动设备 WebApp除了做成响应式设计以外,还可以给移动设备做一套UI,PC端再做一套UI,然后在前台进行判断进行相应的跳转.判断是否是移动设备一般根据浏览器的user ...