Search code examples
gitversion-controlgit-commit

Where can I find snapshot for a specific Git commit?


I have learned that:

  • Git keeps a snapshot for each commit
  • A snapshot is essentially copies of changed files and some references to unchanged files
  • All Git relevant data are stored under .git directory inside the repository directory.
duong2179-mbpro:.git duong2179$ ls -l
total 480
-rw-r--r--    1 duong2179  admin     742 Oct  2 13:03 COMMIT_EDITMSG
-rw-r--r--    1 duong2179  admin   15646 Oct  2 13:28 FETCH_HEAD
-rw-r--r--    1 duong2179  admin      33 Oct  2 13:28 HEAD
-rw-r--r--    1 duong2179  admin      41 Oct  2 13:28 ORIG_HEAD
-rw-r--r--    1 duong2179  admin     849 Oct  2 13:07 config
-rw-r--r--    1 duong2179  admin      73 Apr 24 16:18 description
drwxr-xr-x   17 duong2179  admin     578 Apr 24 16:19 hooks
-rw-r--r--    1 duong2179  admin  185528 Oct  2 13:29 index
drwxr-xr-x    3 duong2179  admin     102 Apr 24 16:18 info
drwxr-xr-x    4 duong2179  admin     136 Apr 24 16:19 lfs
drwxr-xr-x    4 duong2179  admin     136 Apr 24 16:19 logs
drwxr-xr-x  260 duong2179  admin    8840 Aug 19 15:50 objects
-rw-r--r--    1 duong2179  admin   19239 Apr 24 16:19 packed-refs
drwxr-xr-x    5 duong2179  admin     170 Sep 25 16:05 refs
drwxr-xr-x    3 duong2179  admin     102 Aug  5 15:03 subtree-cache

Given a Git commit hash where can I find out the associated snapshot in the .git sub-directory?


Solution

  • You won't find it directly if that commit is packed (delta compressed): see "Git Internals - Packfiles"

    You would need to verify each pack and see if your commit SHA1 is there:

    $ git verify-pack -v .git/objects/pack/pack-978e03944f5c581011e6998cd0e9e30000905586.idx
    2431da676938450a4d72e260db3bf7b0f587bbc1 commit 223 155 12
    69bcdaff5328278ab1c0812ce0e07fa7d26a96d7 commit 214 152 167
    

    You would then need to unpack that pack file.

    If not packed, see "Git Internals - Git Objects"

    $ find .git/objects -type f
    .git/objects/01/55eb4229851634a0f03eb265b69f5a2d56f341 # tree 2
    .git/objects/1a/410efbd13591db07496601ebc7a059dd55cfe9 # commit 3
    

    The commit 3, for instance, has a SHA1 of 1a410efbd13591db07496601ebc7a059dd55cfe9, and has its data in:

    1a/410efbd13591db07496601ebc7a059dd55cfe9 
    

    You can then read the content of that commit metadata with:

    # Windows:
    perl -MCompress::Zlib -e "undef $/; print uncompress(<>)" < 410efbd13591db07496601ebc7a059dd55cfe9 
    
    # Linux
    
    perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < 410efbd13591db07496601ebc7a059dd55cfe9 
    

    You will get:

    commit 349 tree 410efbd13591db07496601ebc7a059dd55cfe9 
    parent 53316403bc0e36f650796d0afdc0687c52357694
    author VonC <VonC@email.com> 1569944592 +0200
    committer VonC <VonC@email.com> 1569944592 +0200
    
    This is a commit comment message