Git 追加代码修改到之前的commit

当我先后提交了dts和config文件后,发现dts文件又有修改?!

  • 假如有如下git仓库
commit a630f3b4f78fcf5baca30f9701ccf21066864c54 (HEAD -> master)
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:48:00 2019 +0800

    commit 3(patch for commit 1)

 git_rebase_test.txt | 1 +
 1 file changed, 1 insertion(+)

commit cebf6f35b0c3f1ea58e9d88d3dd7dd7357e80ad1
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:46:40 2019 +0800

    commit 2

 another_push | 1 +
 1 file changed, 1 insertion(+)

commit 0c6ef53fa39d247e738f7024529ca99a6ebcd6f4
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:43:05 2019 +0800

    commit 1

 git_rebase_test.txt | 1 +
 1 file changed, 1 insertion(+)

commit 89d0fa78fe2cfbaa03629793927e4724ce9cf3c8
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:36:30 2019 +0800

    init

可以看到我想给commit 1追加代码,中间还有另外一个已经push的代码,如何做到不改变commit 2的同时修改commit 1?

操作流程

  • git format-patch -1

使用format-patch得到最新一次commit的补丁: 0001-commit-3-patch-for-commit-1.patch

From a630f3b4f78fcf5baca30f9701ccf21066864c54 Mon Sep 17 00:00:00 2001
From: Ren Jianing <jianing.ren@rock-chips.com>
Date: Tue, 3 Sep 2019 08:48:00 +0800
Subject: [PATCH] commit 3(patch for commit 1)

---
 git_rebase_test.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/git_rebase_test.txt b/git_rebase_test.txt
index a8893de..2c4946d 100644
--- a/git_rebase_test.txt
+++ b/git_rebase_test.txt
@@ -1 +1,2 @@
 commit 1 -> git_rebase_test.txt
+commit 3 -> add patch
--
2.22.0
  • git reset --hard HEAD~1

回滚到commit 2

  • git rebase -i 89d0fa7

rebase 到要修改的上一次提交,这里要修改commit 1,就rebase 到init。

在rebase 窗口中把要修改的提交命令(第一条)改为edit然后退出,此时就可以通过git commit --amend来修改commit

edit 0c6ef53 commit 1
pick cebf6f3 commit 2

在git log中也可以看到base的改变

commit 0c6ef53fa39d247e738f7024529ca99a6ebcd6f4 (HEAD)
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:43:05 2019 +0800

    commit 1

commit 89d0fa78fe2cfbaa03629793927e4724ce9cf3c8
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:36:30 2019 +0800

    init
  • git apply 0001-commit-3-patch-for-commit-1.patch

  • git add -u

  • git rebase --continue

此时rebase操作结束,修改历史提交成功,在git log中可以看到

commit a8ea5da8d809f9317d4ef14f38f88f7f32971e00 (HEAD -> master)
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:46:40 2019 +0800

    commit 2

 another_push | 1 +
 1 file changed, 1 insertion(+)

commit 53b2fdb7952868eef1f64c21caa7975746df2e28
Author: Ren Jianing <jianing.ren@rock-chips.com>
Date:   Tue Sep 3 08:43:05 2019 +0800

    commit 1 with patch

 git_rebase_test.txt | 2 ++
 1 file changed, 2 insertions(+)

在reflog中可以看到rebase的操作历史

a8ea5da (HEAD -> master) HEAD@{2}: rebase -i (finish): returning to refs/heads/master
a8ea5da (HEAD -> master) HEAD@{3}: rebase -i (pick): commit 2
53b2fdb HEAD@{4}: commit (amend): commit 1 with patch
0c6ef53 HEAD@{5}: rebase -i:快进
89d0fa7 HEAD@{6}: rebase -i (start): checkout 89d0fa78
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值