git rebase 让你的一次提交的多个commit和问题一一对应

1、假设当前修改了两个问题,分别提交。git log如下:

$ git log
commit 4d61cd784d1cd498b2a30bc48fcc588a225b15c0
Author: wulan 
Date:   Wed Jul 6 17:16:24 2022 +0800

    [2] add 2.txt
    
commit c2b68eb05d5ea3ad73ec8eb34d0826e53cf07fde
Author: wulan
Date:   Wed Jul 6 17:16:04 2022 +0800
    [1] add 1.txt
    

2、现在发现第一个问题修改不够,继续补充修改。git log如下:

$ git log
commit de57ca0934d9d34f7d2e3e24aedee11788462fc9 (HEAD -> release)
Author: wulan
Date:   Wed Jul 6 17:18:25 2022 +0800

    [1] modify 1.txt
    
commit 4d61cd784d1cd498b2a30bc48fcc588a225b15c0
Author: wulan 
Date:   Wed Jul 6 17:16:24 2022 +0800

    [2] add 2.txt
    
commit c2b68eb05d5ea3ad73ec8eb34d0826e53cf07fde
Author: wulan
Date:   Wed Jul 6 17:16:04 2022 +0800
    [1] add 1.txt
    

3、通过git rebase 调整。

**

交换

**

如果想交换两个commit的位置:

输入 git rebase - i 如下:

pick c2b68eb0 [1] add 1.txt
pick 4d61cd78 [2] add 2.txt
pick de57ca09 [1] modify 1.txt

# Rebase 2fc05ac4..de57ca09 onto 2fc05ac4 (3 commands)
...

光标放到第二行,输入 ddp(vim中快速交换两行的方式,光标所在行跟下面一行进行交换)。
然后输入 shift + ; 接着 wq 保存。再次输入 git log 查询:

commit 02854eb00426bfcd58330575699995e678b552ad (HEAD -> release)
Author: wulan
Date:   Wed Jul 6 17:16:24 2022 +0800

    [2] add 2.txt

commit e7128d8910825c8dfaa3f30e6e06bad402d485e8
Author: wulan 
Date:   Wed Jul 6 17:18:25 2022 +0800

    [1] modify 1.txt

commit c2b68eb05d5ea3ad73ec8eb34d0826e53cf07fde
Author: wulan
Date:   Wed Jul 6 17:16:04 2022 +0800

    [1] add 1.txt

发现commit号已经交换了。也可以直接编辑自主调整顺序,不使用ddp。

**

合并

**

如果不想交换,想要将最新修改的commit合并到[1]中,使用 git commit --amend 是不行的,会合并到最近一次commit [2],满足不了需求,这种情况 也可以使用git rebase。

输入 git rebase - i 如下:(复原了上一步骤)

pick c2b68eb0 [1] add 1.txt
pick 4d61cd78 [2] add 2.txt
pick de57ca09 [1] modify 1.txt

# Rebase 2fc05ac4..de57ca09 onto 2fc05ac4 (3 commands)
...

修改为如下:(pick可以简写为p squash可以简写为s)

pick c2b68eb0 [1] add 1.txt
squash de57ca09 [1] modify 1.txt
pick 4d61cd78 [2] add 2.txt

squash可以将当前commit与上一个commit合并。

然后输入 shift + ; 接着 wq 保存,会弹出commit 信息编辑界面,因为两次commit的commit信息不同,合并后需要重新编辑。

# This is a combination of 2 commits.
# This is the 1st commit message:

[1] add 1.txt

# This is the commit message #2:

[1] modify 1.txt

# Please enter the commit message for your changes. Lines starting
...

修改为:

[1] add 1.txt and modify 1.txt

# Please enter the commit message for your changes. Lines starting
...

然后输入 shift + ; 接着 wq 保存。再次输入 git log 查询:

commit 55c67ccb21d2713ceea51b6949a426b46ae0b611 (HEAD -> release)
Author: wulan
Date:   Wed Jul 6 17:16:24 2022 +0800

    [2] add 2.txt

commit 64907d7200155886c2a80db8b0cd250b06540562
Author: wulan
Date:   Wed Jul 6 17:16:04 2022 +0800

    [1] add 1.txt and modify 1.txt

可以看到已经合并了。

这样即使在完成第二个commit的功能后,又发现第一个commit的修改没有修改完全,也可以补充过去,让你的代码git log更清晰。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值