git (fixup+rebase) 修改某个历史 commit

本文介绍了如何在不破坏其他commits的情况下,修改git中的某个历史commit。通过使用git fixup和rebase命令,结合示例详细阐述了操作流程,包括如何选择commit ID,以及可选的后续配置简化rebase操作。

简述

修改某个历史 commit, 而不破坏其他 commits. 可参考如下做法:

参考做法

以如下 git log 为例,修改 de09c7e 该 commit :

... // latest commit
...
commit de09c7e0768026ba700c8c52a3eb8dafdb79bbe6
Author: xxx
Date:   Tue Nov 10 15:42:26 2020 +0800

    feat: xxx

commit ff3380b6ed6669859fc02fd4c55e381ea7ce0c1f
Author: xxx
Date:   Tue Nov 10 15:37:40 2020 +0800

    fix: xxx
...
... // older commit
  1. 本地针对 commit: de09c7e 修改好

  2. 提交修改到本地仓库

    git add .
    git commit --fixup=de09c7e0768026ba700c8c52a3eb8dafdb79bbe6
    

    这样本地会多出一个 !fixup 开头的 commit.

    --fixup= 后也可以接 HEAD~n, HEAD^n, commidID 等, 即:

    git commit --fixup=HEAD~n
    git commit --fixup=HEAD^n
    git commit --fixup=commitID
    

    commitID 可以只选前 6 位

  3. 完成对 fixup 的 commit 的 rebase

    git rebase -i --autosquash ff3380b6ed6669859fc02fd4c55e381ea7ce0c1f
    # git rebase -i --autosquash 命令中的 commitID 不应是需要 fixup 的 commit, 而是需要比 fixup 更老的 commit。
    

    git rebase -i --autosquash 后也可以接 HEAD~n, HEAD^n, commidID 等, 即:

    git rebase -i --autosquash HEAD~n
    git rebase -i --autosquash HEAD^n
    git rebase -i --autosquash commitID
    

    commitID 可以只选前 6 位

  4. (CTRL+X) 保存即可

(可选) 后续

为了简化后续 rebase, 可配置全局的 rebase.autosquash

git config --global rebase.autosquash true

这样在本地 ~/.gitconfig 文件中会多出如下内容:

[rebase]
  autosquash = true

也可以手动修改 ~/.gitconfig 文件

这样之后如下的操作:

git rebase -i --autosquash HEAD~n

均可以替换为:

git rebase -i HEAD~n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值