为什么即使我在运行 git commit --amend
后没有对提交(消息、文件)进行任何更改,我最新提交的 SHA-1 哈希值也会发生变化?
假设我在命令行运行以下命令。
cd ~/Desktop
mkdir test_amend
cd test_amend
git init
echo 'foo' > test.txt
git add test.txt
git commit -m 'initial commit'
然后,调用
git log --pretty=oneline --abbrev-commit
打印以下信息:
b96a901 initial commit
然后我做
git commit --amend
但我改变主意并决定在上次提交中不更改任何内容。换句话说,我保留了最后一次提交的文件、目录和消息(我只是保存了消息文件并关闭了我的编辑器)。
然后,我做
git log --pretty=oneline --abbrev-commit
又一次,我看到提交的哈希值发生了变化:
3ce92dc initial commit
是什么导致哈希值发生变化?跟commit的时间戳有关系吗?
最佳答案
是的,这是提交时间戳。检查两个提交的内容显示:
$ 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
如果您在原始提交的同一秒内进行了修改,那么您可能会得到相同的哈希值。
关于git - 为什么 git commit --amend 会更改散列,即使我没有进行任何更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23791999/