41

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?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
jub0bs
  • 60,866
  • 25
  • 183
  • 186

3 Answers3

42

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.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 17
    Yep. I can confirm that the hash doesn't change if I run `git commit --amend -m "initial commit"` within the same system-clock second of doing `git commit -m "initial commit"`. Thanks for clearing out my doubt. – jub0bs May 21 '14 at 19:37
  • 6
    @Jubobs - It's worth pointing out that if you do _not_ want the SHA to be touched, you can remove all non-comment lines in the editor, and then save the commit message. git will then note that the commit message was empty, and not do anything. This can be useful if you start committing and then realize that "oops, I should do one more thing first" or whatever (or in this specific use case, perhaps you realize that amending will be bad and you don't want to do it). – Per Lundberg Jan 17 '16 at 13:49
  • 3
    Yep. Or, in vim, use the `:cquit` command to bail out with a nonzero exit code, which also causes git to give up. – Thomas Sep 26 '18 at 10:51
  • 1
    You can also change specify any author date and/or commiter date when you do the commit.... look at `GIT_COMMITTER_DATE` and `git commit --date` – JoelFan Jul 28 '20 at 00:24
11

Following things go in creating commit sha object

  1. tree object reference
  2. parent object reference
  3. author name
  4. author commit timestamp with timezone (e.g for me its +530) [could be different from commiter for example in case of cherry picking]
  5. committer name
  6. commit timestamp with timezone (e.g for me its +530)
  7. commit message

I was trying to figure out why commit SHA ids are different after resetting and again adding the same file with exact same commit message by the same user with same parent and tree object reference.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44
8

Amending a Git commit changes the commit date (which is different from the date you initially see when running git log -- run git log --format=fuller to see the commit date). The commit date is taken into account when creating the commit hash.

mipadi
  • 398,885
  • 90
  • 523
  • 479