Ankur Agarwal
Ankur Agarwal

Reputation: 24768

commit sha1sum gets changed even if I do not modify anything

After a commit I have this in my git log:

    commit 86270b43fe954e78733f1a8f0c4378b954ad9de4
    Author: abc <[email protected]>
    Date:   Sat Aug 31 12:04:32 2013 -0700

        Adding file1 and file2

Then I do

    abc@abc-ubuntu:~/gittest$ git commit --amend

But I save the editor buffer without modifying anything

Now on doing git log again I see

    commit 632c8ddff3fd023e947e76350e0678ba2f04706d
    Author: abc <[email protected]>
    Date:   Sat Aug 31 12:04:32 2013 -0700

        Adding file1 and file2

Notice that the sha1sum of the two commits is different even though I did not change anything during git commit --amend

Upvotes: 2

Views: 73

Answers (2)

user743382
user743382

Reputation:

You did change something: you changed the commit time. You can see this with

git show --format=fuller 86270b43fe954e78733f1a8f0c4378b954ad9de4
git show --format=fuller 632c8ddff3fd023e947e76350e0678ba2f04706d

Look at the two CommitDates, they won't match.

As hinted in mvp's answer, if you do manage to duplicate the commit date/time, you would get the same sha1. You can override the commit time to any value you wish, including the exact commit time recorded in the original commit, by setting the GIT_COMMITTER_DATE environment variable:

GIT_COMMITTER_DATE=$(git log -1 --format=%cd) git commit --amend

Now, if you don't change the commit message, you should get the exact same commit. But unless you have a reason for not recording the fact that you recommitted, you should probably just stick with the defaults.

Upvotes: 3

mvp
mvp

Reputation: 116397

Yes, this is correct - note that when you git commit --amend without modifying any files or commit message, you are still creating new commit object, which has different timestamp in it, and thus different SHA-1 checksum. This is because each commit has two timestamps: when it was created by an author (one that is shown by default in git log), and when it was committed (hidden by default).

If you were somehow able to completely duplicate absolutely everything in commit object, including timestamps, then this would not have happened.

Upvotes: 2

Related Questions