如何在不更改连续提交的 SHA-1 的情况下修改特定提交?

How to modify a specific commit without changing SHA-1 of consecutive commits?

请假设一个分支:

  1. HEAD
  2. Commit-3
  3. Commit-2
  4. Commit-1

特定提交的内容,比如 Commit-1,可以按如下详述进行修改:

How to modify a specified commit?

How to amend older Git commit?

然而,如 here.

所述,此类尝试“ 将更改提交的 SHA-1 以及所有子项

问题:

是否可以在不更改 连续提交 的 SHA-1 的情况下修改 Commit-1? (可以更改 Commit-1 的 SHA-1。)

如果不更改 Commit-1 的所有子项和后代提交的 SHA-1,则无法更改 Commit-1。这是因为提交的 SHA-1 是使用父提交的 SHA-1 作为计算的一部分来计算的。在 Git 中,更改提交需要为后代创建新的提交。在您的示例中,Commit-1 的后代是 Commit-2Commit-3HEAD

可以使用 git replace <commit> <other commit>

https://git-scm.com/docs/git-replace