git 如何合并两个或多个commit成为一个commit

一、场景

有时候我们在用git提交代码之后,突然发现有些细节的小问题,需要修改并再次提交,但是这次的提交又没有必要作为一个单独的commit,也不想让领导看到这多次的提交,从而觉得这开发太不认真,这时候就需要使用git命令把多次的commit合并成一个commit。

二、 操作步骤

1、打开git bash 命令窗口,到对应的项目下面,输入命令:git rebase -i HEAD~2 , 其中HEAD~2是指历史的前两个提交,可以根据自己的情况调整,比如像合并前三个,就改为HEAD~3。

输入命令后,会出来vim编辑界面,如下:

pick d2a6e0d1a5c MN-16032 add audit log for contact delete via manager portal
pick a15751a94c0 MN-16032 optimize saveAuditLogForContacts to handle add/update operation
# Rebase 40b7ebb2ef8..a15751a94c0 onto 40b7ebb2ef8 (2 commands)
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message

2、 修改第二行的pick为s,也就是squash合并的意思,退出编辑,此时会执行合并,并再次弹出vim编辑界面,如下:

1 # This is a combination of 2 commits.
2 # This is the 1st commit message:
3
4 change 1: rename the field
5
6 # This is the commit message #2:
7
8 change 2: add log info
9
10 # Please enter the commit message for your changes. Lines starting
11 # with '#' will be ignored, and an empty message aborts the commit.

将上边的change 1 和change 2 合并成一个如:

1 # This is a combination of 2 commits.
2 # This is the 1st commit message:
3
4 change 1: rename the field and add log info

10 # Please enter the commit message for your changes. Lines starting
11 # with '#' will be ignored, and an empty message aborts the commit.

保存并退出编辑

3、输入命令 git push origin mybranch -f ,这里的mybranch填你当前的分支

4、这样就将两个commit合并成了一个commit。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值