合并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. Android的onLayout、layout方法讲解

    onLayout方法是ViewGroup中子View的布局方法,用于放置子View的位置.放置子View很简单,只需在重写onLayout方法,然后获取子View的实例,调用子View的layout方 ...

  2. java的关键字与保留字

    1,Java 关键字列表 (依字母排序 共50组): abstract, assert, boolean, break, byte, case, catch, char, class, const(保 ...

  3. linux sendmail 发送缓慢的问题

    这个问题关键就是hosts里面对于本机host的设置有问题,你可以看下/var/log/mail.log retry 和 sleeping,再加上其他时间,用sendmail发送邮件等这么久,太累了吧 ...

  4. 支付宝支付集成过程中如何生成商户订单号(out_trade_no)

    out_trade_no是指商户网站唯一订单号,在商户端唯一,每个商户订单号会对应一个支付宝订单号 ,此订单号由珊瑚自己生成,商户订单号要求64个字符以内.可包含字母.数字.下划线:需保证在商户端不重 ...

  5. ELASTIC API

    运维常用API. curl -XGET 'localhost:9200/_cat/indices?v&pretty' #查看索引 curl -XGET 'localhost:9200/_cat ...

  6. 如何对MongoDB 3.2.7进行用户权限管理配置

    转自:https://www.jianshu.com/p/a4e94bb8a052 上次写了一篇在CentOS7上源码安装MongoDB 3.2.7,完成了MongoDB 3.2.7的安装,但需要应用 ...

  7. C#后台解析 json 动态解析 通用(Dictionary)

    Dictionary<string, object> suggestions = JSONSerializer.Deserialize<Dictionary<string, o ...

  8. AngularJS中页面呈现html代码段

    如何在页面呈现一段html代码段呢? 在textarea中我们这样写: <textarea name="" id="" cols="30&quo ...

  9. android: 接收系统广播

    Android 内置了很多系统级别的广播,我们可以在应用程序中通过监听这些广播来得到 各种系统的状态信息.比如手机开机完成后会发出一条广播,电池的电量发生变化会发出一 条广播,时间或时区发生改变也会发 ...

  10. 你还记得当初为什么进入IT行业吗?

    说到这个问题,小编相信不少童鞋开始忆往昔峥嵘岁月,那个少年为了心中的改变世界的理想,进入了这个行业,但是呢,有一群人画风就不一样了,他们进入IT行业,完全只是是因为.... 小时候广告看多了....: ...