本文介绍了为什么git commit --amend即使不做任何更改也会更改哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么即使我在运行 git commit --amend



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

  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初始提交

然后我做

  git commit --amend 

但我改变主意并决定不要改变上次提交中的任何内容。换句话说,我保留最后提交的文件,目录和消息(我只保存消息文件并关闭编辑器)。



然后,我做

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

再次看到提交的散列已经改变:

  3ce92dc初始提交

导致哈希变化的原因是什么?它是否与提交的时间戳有关?

解决方案

是的,它是提交时间戳。检查两次提交的内容显示:

  $ git的猫文件提交82c7363bcfd727fe2d6b0a98412f71a10c8849c9 
树d87cbcba0e2ede0752bdafc5938da35546803ba5
作者Thomas< xxx> 1400700200 +0200
提交者Thomas< xxx> 1400700200 +0200

你好

$ git的猫文件提交7432fcf82b65d9d757efd73ef7d6bff4707f99bd
树d87cbcba0e2ede0752bdafc5938da35546803ba5
的作者托马斯< XXX和GT; 1400700200 +0200
提交者Thomas< xxx> 1400700214 +0200

hello

如果您在原来的提交,大概你会得到相同的散列。


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?

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?

解决方案

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.

这篇关于为什么git commit --amend即使不做任何更改也会更改哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 20:09