Git Object: Tree

tree

A git tree object is very similar to a filesystem directory.

Filesystem directories refer to other directories and files, while git trees refer to other git trees and git blobs:

Filesystem Directories Refer To: Git Trees Refer To:
1. Other directories 1. Other git trees
2. Files 2. Git blobs (files)

Example: Filesystem Directories and Files; Git Trees and Files

Diagram 1: The filesystem

In Diagram 1, below, an example filesystem directory and files is shown.

  • There are 3 directories: src, docs and the top of the working directory.
  • There are 4 files named README, hello.c, hello.py and hello. The contents of the files are shown in the grey boxes below the filenames.

The commands for creating the files and directories, and adding the files to the git object store, is shown here.

Diagram 2: The Git Object Store

Diagram 2 shows how the files and directories are stored as git trees and blobs in the git object store, after the user adds and commits the files and directories to the git repository.

Diagram 2 shows the top of the git tree’s hash is 39179a1 (lower left-hand corner of the highest green triangle). That git tree refers to 3 objects:

  1. The blob that contains the contents of the README file.
  2. The tree that contains the contents of the src directory.
  3. The tree that contains the contents of the doc directory.

The first few bytes of the hash of each object is shown below the object.

Diagram 3: The Object Hashes

Diagram 3 adds annotation that shows how each object’s hash is referred to in the git trees.

Diagram 4: Objects Without Name Labels

Diagram 4 shows the same git object store, but without each of the objects including their name above the left side of the object. This more accurately that objects are stored by their hash. Git knows the name of an object when the object is referred to in a git tree: The git tree includes the object’s hash and its name.

In the next section, the commands for both creating the above git object store and displaying the hashes are shown.

Next: Git Session: The Commands Used For The Examples On This Page
Previous: The Git Blob Object

Related: