linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Object model/tree

kb/objects ── Object model ── intermediate

Tree

A Git object that holds the listing of one directory: entries of the form `(mode, type, SHA, name)`. It references other tree objects recursively for subdirectories.

view as markdownaka: git-tree, tree-object

A tree object describes the contents of one directory. Each line in a tree is one entry:

100644 blob 5f7e9c12...    README.md
100644 blob 8a3f2e91...    index.html
040000 tree e2b5a91f...    images
100644 blob b1d4a7e0...    style.css

Four columns:

  • Mode - 100644 is a regular file, 100755 is executable, 040000 is a subdirectory, 120000 is a symlink, 160000 is a gitlink (submodule). Git stores the mode as a number; git ls-tree always prints it as six digits with a leading zero.
  • Type - blob for a file, tree for a subdirectory.
  • SHA - a pointer to the blob or tree object.
  • Name - the filename or directory name within this directory.

The file content lives in a blob. The tree links a name to that content.

Recursive structure

A tree entry of type tree points to another tree object for a subdirectory. This builds a hierarchy of tree objects that mirrors the project's directory tree:

root tree
   ├── blob README.md
   ├── tree images/
   │      ├── blob logo.png
   │      └── blob banner.jpg
   └── blob style.css

When a tree is created

On git commit. Before the commit, no tree exists; there is only .git/index, which holds a flat list of paths. git write-tree converts the index into a hierarchy of tree objects and returns the SHA of the root.

Inspecting tree contents

bash
git cat-file -p <tree-sha>
git ls-tree HEAD              # tree of the latest commit
git ls-tree -r HEAD           # recursive, flat list of all files

Deduplication at the tree level

If a subdirectory has not changed between two commits, its tree object is identical. The parent tree points to the same SHA. Git deduplicates not only individual files but entire subdirectories.

§ команды

bash
git cat-file -p HEAD^{tree}

Show the contents of the latest commit's root tree

bash
git ls-tree HEAD

List the root directory tree of the latest commit

bash
git ls-tree -r HEAD

Recursively list all files from the tree

bash
git write-tree

Build a tree from the current index

§ см. также

  • blobBlobA Git object that stores the content of a single file. Just bytes, no name, no permissions, no date. The filename lives in the `tree`, not in the blob.
  • commitCommitA Git object: a snapshot of the entire project (via a tree) plus metadata including author, committer, date, parents, and message. The SHA of a commit includes the parent's SHA, which makes history cryptographically linked.
  • sha1SHA-1 in GitThe 40-character hash Git uses to address objects. Computed from content with a type prefix. Broken cryptographically in 2017 (SHAttered), but still the default in Git as migration to SHA-256 moves slowly.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies