即使我不做任何更改,为什么 git commit --amend 也会更改哈希?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23791999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:03:05  来源:igfitidea点击:

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

gitshaamend

提问by jub0bs

Why does the SHA-1 hash of my latest commit change even if I don't make any changes to the commit (message, files) after running git commit --amend?

为什么即使我在运行后没有对提交(消息、文件)进行任何更改,我最新提交的 SHA-1 哈希值也会更改git commit --amend

Say I run the following at the command line.

假设我在命令行运行以下命令。

cd ~/Desktop
mkdir test_amend
cd test_amend
git init
echo 'foo' > test.txt
git add test.txt
git commit -m 'initial commit'

Then, invoking

然后,调用

git log --pretty=oneline --abbrev-commit

prints the following message:

打印以下消息:

b96a901 initial commit

I then do

然后我做

git commit --amend

but I change my mind and decide not to change anything in the last commit. In other words, I leave the files, directories, and message of the last commit untouched (I just save the message file and close my editor).

但我改变了主意,决定在最后一次提交中不做任何更改。换句话说,我将上次提交的文件、目录和消息保持不变(我只是保存消息文件并关闭我的编辑器)。

Then, I do

然后,我做

git log --pretty=oneline --abbrev-commit

one more time, I see that the hash of the commit has changed:

又一次,我看到提交的哈希值发生了变化:

3ce92dc initial commit

What causes the hash to change? Does it have to do with the time stamp of the commit?

是什么导致哈希改变?它与提交的时间戳有关吗?

回答by Thomas

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.

如果您在原始提交的同一秒内修改,大概您会得到相同的哈希值。

回答by Abhijit Mazumder

Following things go in creating commit sha object

在创建提交 sha 对象时进行以下操作

  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
  1. 树对象引用
  2. 父对象引用
  3. 作者姓名
  4. 作者提交时间戳与时区(例如对我来说是 +530)[可能与提交者不同,例如在樱桃采摘的情况下]
  5. 提交者姓名
  6. 使用时区提交时间戳(例如对我来说是 +530)
  7. 提交消息

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 .

我试图弄清楚为什么在重置并再次添加具有相同父级和树对象引用的同一用户的完全相同提交消息的相同文件后,为什么提交 SHA id 不同。

回答by mipadi

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=fullerto see the commit date). The commit date is taken into account when creating the commit hash.

修改 Git 提交会更改提交日期(这与您在运行时最初看到的日期不同git log——运行git log --format=fuller以查看提交日期)。创建提交哈希时会考虑提交日期。