博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git合并最近的commit
阅读量:6281 次
发布时间:2019-06-22

本文共 3063 字,大约阅读时间需要 10 分钟。

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

1. git rebase

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

commit 0bca654809387dc226cfc4ff872f65601809f485Author: CasiaFan 
Date: Wed Aug 16 13:13:12 2017 +0800 fix typocommit b5bf9998e40dc165d7e3055bae47172de11225d4Author: CasiaFan
Date: Wed Aug 16 10:02:30 2017 +0800 add random vertical flip during image preprocessingcommit 70a4958184106b9ce848da34be34bdf0dbf36450Author: CasiaFan
Date: Wed Aug 16 09:58:55 2017 +0800 step by step runningcommit d68608c2dbd41a1b89363f4b743bc5464b6749beAuthor: CasiaFan
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 descriptionpick 70a4958 step by step runningpick b5bf999 add random vertical flip during image preprocessingpick 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 7f774d88eb6884c97c8b3c05a9268afa581d5b57Author: Unknown 
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/

转载地址:http://ipxva.baihongyu.com/

你可能感兴趣的文章
npoi实现 从固定的行读取数据作为表头并返回datable
查看>>
【Hibernate学习】 ——ORM(三)
查看>>
概率dp入门
查看>>
dotfuscator初步
查看>>
Ubuntu各个版本的介绍
查看>>
【leetcode】Pascal's Triangle I & II (middle)
查看>>
SQL Server
查看>>
GitHub详细教程
查看>>
【书评:Oracle查询优化改写】第三章
查看>>
Python 内置彩蛋
查看>>
SQLServer 之 常用函数及查看
查看>>
FrameWork中SQLServer数据源使用宏函数出错解决办法
查看>>
[.net 面向对象编程基础] (8) 基础中的基础——修饰符
查看>>
如何在plSql查询数据查出的数据可编辑
查看>>
2015年第11本:代码整洁之道Clean Code
查看>>
PHP 错误与异常 笔记与总结(11 )register_shutdown_function() 函数的使用
查看>>
talend 将hbase中数据导入到mysql中
查看>>
内置在虚拟机上64位操作系统:该主机支持 Intel VT-x,但 Intel VT-x 残
查看>>
Material Design练习
查看>>
[译] 二、开始iOS编程之前,你还需要做什么?
查看>>