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
* b3e19a3 2013-10-06 | Added a Rakefile. (HEAD, master) [Ismail Dhorat]

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 b3e19a3
commit
$ git cat-file -p b3e19a3
tree 2ab5200289e9065e64e20261dc5791e3243506eb
parent 14f09c0be1109788d29b913858a5557a9896230e
author Ismail Dhorat <ismail@codiez.co.za> 1381062894 +0200
committer Ismail Dhorat <ismail@codiez.co.za> 1381062894 +0200

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 2ab5200
100644 blob 28e0e9d6ea7e25f35ec64a43f569b550e8386f90	Rakefile
040000 tree d76f439c82a941fd42a84c9aa855782b65f4cc84	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 d76f439
100644 blob 4fada682816a5375519a95df5be6bc2d4b6ffc0a	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 4fada68
# Default is World
# Author: Ismail Dhorat (ismail@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