How does git assure that commit SHA keys for identical operations/data are still unique?

后端 未结 4 1861
Happy的楠姐
Happy的楠姐 2021-01-17 22:22

If I create a file foo with touch foo and then run shasum foo it will print out

da39a3ee5e6b4b0d3255bfef95601890afd80709

4条回答
  •  离开以前
    2021-01-17 22:56

    This Gist from Carl Mäsak explains it better than I ever could:

    https://gist.github.com/masak/2415865

    alex@yuzu:~/foo$ git show HEAD
    commit 7438e0a18888854650e6a53a9a5d823d6382de45
    Author: Foo Bar 
    Date:   Wed Jul 30 19:35:32 2014 -0700
    
        foo
    
    diff --git README README
    new file mode 100644
    index 0000000..e69de29
    

    Which is the SHA-1 checksum of "commit\0" followed by a character count (length) followed by git cat-file commit HEAD.

    alex@yuzu:~/foo$ git cat-file commit HEAD
    tree 543b9bebdc6bd5c4b22136034a95dd097a57d3dd
    author Foo Bar  1406774132 -0700
    committer Alex Balhatchet  1406774132 -0700
    
    foo
    

    Put it all together and...

    alex@yuzu:~/foo$ (printf "commit %s\0" $(git cat-file commit HEAD | wc -c); git cat-file commit HEAD) | sha1sum
    7438e0a18888854650e6a53a9a5d823d6382de45  -
    

    The sha1sum output matches the commit SHA-1!

提交回复
热议问题