合并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 +0800fix typocommit b5bf9998e40dc165d7e3055bae47172de11225d4
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Wed Aug 16 10:02:30 2017 +0800add random vertical flip during image preprocessingcommit 70a4958184106b9ce848da34be34bdf0dbf36450
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Wed Aug 16 09:58:55 2017 +0800step by step runningcommit d68608c2dbd41a1b89363f4b743bc5464b6749be
Author: CasiaFan <fanzongshaoxing@gmail.com>
Date:   Mon Aug 14 15:52:47 2017 +0800modify 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 +0800add random vertical flip for image preprocessingtestobject detection api usage stepmodify descriptionstep by step runningadd random vertical flip during image preprocessingfix 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/

转载于:https://www.cnblogs.com/arkenstone/p/7373540.html

Git合并最近的commit相关推荐

  1. git合并多个commit成为一个commit

    git合并多个commit成为一个commit 在git使用中,可能会在合并代码的时候有大量的commit,如何能在合并分支的时候将许多commit转化为1个commit呢,可以使用以下命令: git ...

  2. git合并多个commit

    在使用 Git 作为版本控制的时候,我们可能会由于各种各样的原因提交了许多临时的 commit,而这些 commit 拼接起来才是完整的任务.那么我们为了避免太多的 commit 而造成版本控制的混乱 ...

  3. IDEA中Git合并冲突

    1.美图 2.背景 IDEA中Git合并冲突 先commit本地修改的文件到本地repository pull源码,因为存在代码冲突,所以接下来会自动弹出merge融合窗口,如下图:

  4. Git : 合并 commit 保持分支干净整洁

    本文的读者需要已经了解 基本的 Git 操作和开发流程. 在我们开发完分支后,一般分支上会有很多 commit -- 少不了诸如 "fix typo", "sth wro ...

  5. IDEA GIT 合并commit

    步骤 前提:当前分支在对应commit所在的分支下 打开IDEA的GIT侧边栏,左侧选择本地的分支或者远端的分支,中间将展示提交的commit,右侧将出现每一个commit对应的修改文件 选择要合并的 ...

  6. GIT合并特定commit

    Git合并特定commits 到另一个分支 标签: gitmerge合并特定commit单个commit 2014-12-25 14:13 21570人阅读 评论(4) 收藏 举报 分类: Git(1 ...

  7. git 几个commit点合并成一个commit点

    在用git做版本控制器的时候,经常会遇到以下情况: 1.在做1个功能的时候,你自己觉得代码没问题了,就本地commit,然后提交代码,在gitlab上发起和并请求,老大看完之后,觉得你还有修改的地方, ...

  8. Git合并和变基简介:它们是什么,以及如何使用它们

    by Vali Shah 通过瓦利沙阿 Git合并和Git变基简介:它们做什么以及何时使用它们 (An Introduction to Git Merge and Git Rebase: What T ...

  9. 简单介绍Git合并分支的流程步骤

    这篇文章主要介绍了详解Git合并分支的流程步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 正常合并分支dev到master流 ...

最新文章

  1. 服务器上安装运行fastqc
  2. [Vue CLI 3] 插件开发之 registerCommand 到底做了什么
  3. 嵌入式linux笔记,嵌入式linux学习笔记(一)----嵌入式操作系统
  4. Photoshop 融合属性 Unity Shader
  5. 【Android RTMP】RTMP 数据格式 ( FLV 视频格式分析 | 文件头 Header 分析 | 标签 Tag 分析 | 视频标签 Tag 数据分析 )
  6. 同一条sql insert 有时快有时慢 引发的血案
  7. java xml 反射_Java 读取XML文件以及Java 的反射机制实现
  8. 银盒子扫码下单在线订单开启商品售卖时段使用说明
  9. 《计算机应用基础》试卷,《计算机应用基础》试卷(二)
  10. linux同一台机器安装两台nginx
  11. matlab符号运算与数值运算的转化
  12. Sql Sugar使用仓储实现增删改查
  13. WBS(Work Breakdown Structure)
  14. 南大通用GBase8s 常用SQL语句(150)
  15. STING 与 cGAS的结合导致TBK1 激酶募集和活化
  16. 2023真无线蓝牙耳机怎么选?值得入手的蓝牙耳机推荐
  17. 完全卸载3dmax(以及桌子全家产品)
  18. 软件工程导论—软件与软件工程
  19. mac mysql 移动硬盘_Mac下无法推出硬盘
  20. 领域驱动设计-领域建模

热门文章

  1. table中的td表示table data(表格数据),tr表示table row(表格行),th表示table head(表格头)
  2. java-将xlsx(excel)文件转换成json
  3. 判断 Map 中是否包含指定的 key 和 value
  4. git命令之git rebase 的用法
  5. 网络编程6_multiprocess模块.锁.队列
  6. 【2-SAT】URAL - 2089 - Experienced coach
  7. Parasoft C++test使用教程:执行测试用例(上)
  8. Maven 环境快速搭建
  9. Ansible8:Playbook循环
  10. mysql计算经纬度亮点之间的距离