2

Try this:

mkdir a  
cd a
git init
touch f1
git add f1
git commit -m "Commit 1"
git log

Note the SHA. Then do

git commit --amend -m "Commit 1"
git log

Note that the SHA has changed, although nothing that I can see has changed about the commit itself (not even the commit date.)

What is different about these two commits that generates a new SHA?

Michael Gundlach
  • 106,555
  • 11
  • 37
  • 41

1 Answers1

4

I ran through your example locally. If you use git cat-file to examine the commit objects...

After the first commit:

$ git cat-file -p f9afca7508e2d97ca4babfb897fa5acefe67af54
tree 56e3dd6f60494c9bbe56ea178b9a86c91d3139c6
author Lars Kellogg-Stedman <lars@example.com> 1376499716 -0400
committer Lars Kellogg-Stedman <lars@example.com> 1376499716 -0400

Commit 1

After amending:

$ git cat-file -p 976778e7f58c4b2f89b4f652e89c420e1266d297
tree 56e3dd6f60494c9bbe56ea178b9a86c91d3139c6
author Lars Kellogg-Stedman <lars@example.com> 1376499716 -0400
committer Lars Kellogg-Stedman <lars@example.com> 1376499733 -0400

Commit 1

Note that these two commits have different timestamps for the "commiter" metadata. Since the data is different, the commit ID is different. If you change the data, you change the SHA1 of the object.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Right, any operation that 're-commits' something will change the committer timestamp. I don't think anything ever changes the author's, so you can always see who did the work of a commit, and who last moved/reused the commit through some secondary operation. It only keeps one level of this change in the new commit's data (the rest is still there in the other commit objects, though unreachable from the newer commit). `rebase`, `cherry-pick`, `commit --amend`, etc., all make a new commit, changing at least the committer date., Some might change the tree and/or parent tree pointed to as well. – Gary Fixler Aug 14 '13 at 17:48
  • I had not known that there is a separate date for the author's commit and the committer's commit. That explains it. Thank you! – Michael Gundlach Aug 15 '13 at 14:20