简述
修改某个历史 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
- 本地针对 commit:
修改好de09c7e
- 提交修改到本地仓库
这样本地会多出一个git add . git commit --fixup=de09c7e0768026ba700c8c52a3eb8dafdb79bbe6
开头的 commit.!fixup
后也可以接 HEAD~n, HEAD^n, commidID 等, 即:--fixup=
commitID 可以只选前 6 位git commit --fixup=HEAD~n git commit --fixup=HEAD^n git commit --fixup=commitID
- 完成对 fixup 的 commit 的 rebase
git rebase -i --autosquash ff3380b6ed6669859fc02fd4c55e381ea7ce0c1f # git rebase -i --autosquash 命令中的 commitID 不应是需要 fixup 的 commit, 而是需要比 fixup 更老的 commit。
后也可以接 HEAD~n, HEAD^n, commidID 等, 即:git rebase -i --autosquash
commitID 可以只选前 6 位git rebase -i --autosquash HEAD~n git rebase -i --autosquash HEAD^n git rebase -i --autosquash commitID
- (CTRL+X) 保存即可
(可选) 后续
为了简化后续 rebase, 可配置全局的
rebase.autosquash
这样在本地
~/.gitconfig
文件中会多出如下内容:
[rebase]
autosquash = true
也可以手动修改 ~/.gitconfig
文件
这样之后如下的操作:
git rebase -i --autosquash HEAD~n
均可以替换为:
git rebase -i HEAD~n