Git Internals:

Working directly with Git Objects

Goals

  • Explore the structure of the object store
  • Learn how to use the SHA1 hashes to find content in the repository

Now let’s use some tools to probe git objects directly.

Finding the Latest Commit

Execute:

  1. git hist --max-count=1

This should show the latest commit made in the repository. The SHA1 hash on your system is probably different from what is on mine, but you should see something like this.

Output:

  1. $ git hist --max-count=1
  2. * 5aec14d 2020-06-20 | Added a Rakefile. (HEAD -> master) [Jim Weirich]

Dumping the Latest Commit

Using the SHA1 hash from the commit listed above …

Execute:

  1. git cat-file -t <hash>
  2. git cat-file -p <hash>

Here’s my output …

Output:

  1. $ git cat-file -t 5aec14d
  2. commit
  3. $ git cat-file -p 5aec14d
  4. tree 096b74c56bfc6b40e754fc0725b8c70b2038b91e
  5. parent 721b979f776a270ff24ffc58f1075f0dc1c06bc1
  6. author Jim Weirich <jim (at) edgecase.com> 1592681829 +0100
  7. committer Jim Weirich <jim (at) edgecase.com> 1592681829 +0100
  8. Added a Rakefile.

NOTE: If you defined the ‘type’ and ‘dump’ aliases from the aliases lab, then you can type git type and git dump rather than the longer cat-file commands (which I never remember).

This is the dump of the commit object that is at the head of the master branch. It looks a lot like the commit object from the presentation earlier.

Finding the Tree

We can dump the directory tree referenced in the commit. This should be a description of the (top level) files in our project (for that commit). Use the SHA1 hash from the “tree” line listed above.

Execute:

  1. git cat-file -p <treehash>

Here’s what my tree looks like…

Output:

  1. $ git cat-file -p 096b74c
  2. 100644 blob 28e0e9d6ea7e25f35ec64a43f569b550e8386f90 Rakefile
  3. 040000 tree e46f374f5b36c6f02fb3e9e922b79044f754d795 lib

Yep, I see the Rakefile and the lib directory.

Dumping the lib directory

Execute:

  1. git cat-file -p <libhash>

Output:

  1. $ git cat-file -p e46f374
  2. 100644 blob c45f26b6fdc7db6ba779fc4c385d9d24fc12cf72 hello.rb

There’s the hello.rb file.

Dumping the hello.rb file

Execute:

  1. git cat-file -p <rbhash>

Output:

  1. $ git cat-file -p c45f26b
  2. # Default is World
  3. # Author: Jim Weirich (jim@somewhere.com)
  4. name = ARGV.first || "World"
  5. puts "Hello, #{name}!"

There you have it. We’ve dumped commit objects, tree objects and blob objects directly from the git repository. That’s all there is to it, blobs, trees and commits.

Explore On You Own

Explore the git repo manually on your own. See if you can find the original hello.rb file from the very first commit by manually following the SHA1 hash references starting in the latest commit.