Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...