lab 23 Git Internals:
Working directly with Git Objects

Goals

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

Finding the Latest Commit 01

Execute:

git hist --max-count=1

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

Output:

$ git hist --max-count=1
* 96ee164 2013-04-13 | Added a Rakefile. (HEAD, master) [Jim Weirich]

Dumping the Latest Commit 02

Using the SHA1 hash from the commit listed above …

Execute:

git cat-file -t <hash>
git cat-file -p <hash>

Here’s my output …

Output:

$ git cat-file -t 96ee164
commit
$ git cat-file -p 96ee164
tree 096b74c56bfc6b40e754fc0725b8c70b2038b91e
parent 0f36766e05bc55d765ec8afe288430edc69fceea
author Jim Weirich <jim (at) neo.com> 1365880844 -0400
committer Jim Weirich <jim (at) neo.com> 1365880844 -0400

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 03

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:

git cat-file -p <treehash>

Here’s what my tree looks like…

Output:

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

Yep, I see the Rakefile and the lib directory.

Dumping the lib directory 04

Execute:

git cat-file -p <libhash>

Output:

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

There’s the hello.rb file.

Dumping the hello.rb file 05

Execute:

git cat-file -p <rbhash>

Output:

$ git cat-file -p c45f26b
# Default is World
# Author: Jim Weirich (jim@somewhere.com)
name = ARGV.first || "World"

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 06

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.

Table of Contents