Why does git commit --amend change the hash even if I don't make any changes?

孤人 提交于 2019-11-28 09:38:30

Yes, it's the commit timestamp. Inspecting the contents of the two commits reveals:

$ git cat-file commit 82c7363bcfd727fe2d6b0a98412f71a10c8849c9
tree d87cbcba0e2ede0752bdafc5938da35546803ba5
author Thomas <xxx> 1400700200 +0200
committer Thomas <xxx> 1400700200 +0200

hello

$ git cat-file commit 7432fcf82b65d9d757efd73ef7d6bff4707f99bd
tree d87cbcba0e2ede0752bdafc5938da35546803ba5
author Thomas <xxx> 1400700200 +0200
committer Thomas <xxx> 1400700214 +0200

hello

If you amended in the same second as the original commit, presumably you'd get the same hash.

Following things go in creating commit sha object

  1. tree object reference
  2. parent object reference
  3. author name
  4. author commit timestamp with timezone (e.g for me its +530) [could be different from commiter for example in case of cherry picking]
  5. committer name
  6. commit timestamp with timezone (e.g for me its +530)
  7. commit message

I was trying to figure out why commit SHA ids are different after resetting and again adding the same file with exact same commit message by the same user with same parent and tree object reference .

Amending a Git commit changes the commit date (which is different from the date you initially see when running git log -- run git log --format=fuller to see the commit date). The commit date is taken into account when creating the commit hash.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!