0

Please assume a branch:

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

The content of a specific commit, say Commit-1, can be modified as elaborated below:

How to modify a specified commit?

How to amend older Git commit?

Nevertheless, such attempts "will change the SHA-1 of the commit as well as all children" as stated here.

Question:

Is it possible to modify Commit-1 without changing the SHA-1 of the consecutive commits? (The SHA-1 of Commit-1 can be changed.)

Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34
  • 1
    The fact that you *cannot* do this is a feature. It means that someone can GPG-sign a particular tag and thus endorse every commit leading up to and including the one mentioned in that tag. – torek Nov 23 '19 at 17:25

2 Answers2

4

It is NOT possible to change Commit-1 without changing the SHA-1 of all child and descendent commits of Commit-1. This is because the SHA-1 of a commit is calculated using the parent commit(s)' SHA-1 as part of the calculation. In Git, changing a commit requires creating new commits for descendants. In your example, the descendants of Commit-1 are Commit-2, Commit-3 and HEAD.

David Sugar
  • 1,042
  • 6
  • 8
0

It is possible to do that using git replace <commit> <other commit>

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

Andrew Xu
  • 71
  • 1
  • 2