Git合并最近的commit
发布日期:2021-08-21 13:17:25 浏览次数:35 分类:技术文章

本文共 3117 字,大约阅读时间需要 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/

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

转载地址:https://blog.csdn.net/weixin_33720078/article/details/93609217 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SQL高级语言
下一篇:RK30SDK系统重启源码分析

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年05月06日 03时34分59秒