git - 为什么 git commit --amend 会更改散列,即使我没有进行任何更改?

标签 git sha git-amend

为什么即使我在运行 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/

相关文章:

Git - 将更改从子模块推送到主模块?

security - SHA256 安全问题

git - git commit --amend 究竟是如何工作的?

Git:如何编辑/改写 merge 提交的消息?

Git:以下未跟踪的工作树文件将被 merge 覆盖:upload/.DS_Store

git - 使用远程主机 gitlab 从 Windows 部署 capifony 时权限被拒绝(公钥)

git - .gitignore 的奇怪行为

android - 您指定的指纹已被此项目或其他项目中的 Android OAuth2 客户端 ID 使用

ios - 当我想将新文件提交到 git 时,如何使用 libgit2 获取 SHA 值

Git commit --amend merge 两个提交