HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
4 Topics
Chapter•45 Minutes

Advanced Git Internals

Explore Git objects, hashes, refs, HEAD, the index, packfiles, and the mechanics behind Git's distributed architecture.

Git Objects

Git stores data using objects. The core object types are blob, tree, commit, and tag. Blobs contain file contents, trees describe directory structures, commits point to trees and parent commits, and annotated tags can point to Git objects with metadata.

Objects are addressed by cryptographic object IDs.

Git Object Model
Inspect an Object
git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -t HEAD^{tree}
git rev-parse HEAD
💡 cat-file can inspect Git objects. rev-parse resolves references such as HEAD to their object IDs.
Key Takeaways
  • Git stores content as objects rather than storing a sequence of file copies.
  • Commits point to trees and parent commits.
  • Object IDs provide stable content-addressed references.
Knowledge Check

Which Git object stores file contents?

Refs and HEAD

References, or refs, are names that point to Git objects. Branches and tags are examples of refs. HEAD is a special symbolic reference that usually points to the current branch.

This design explains why creating a branch is inexpensive: a branch is essentially a small reference rather than a copy of the entire repository.

Inspect References
git show-ref
git rev-parse HEAD
git symbolic-ref HEAD
git branch --show-current
💡 These commands reveal references, the current commit, the symbolic HEAD target, and the active branch.
Key Takeaways
  • Branches and tags are references.
  • HEAD normally points symbolically to the current branch.
  • References make branch creation and movement inexpensive.
Knowledge Check

Why is creating a Git branch usually cheap?

The Git Index

The index is the data structure used as Git's staging area. It records the content that will form the next tree when a commit is created. Commands such as git add update the index.

Understanding the index explains why staging and committing are separate operations.

Inspect the Index
git add src/app.js
git ls-files --stage

git diff
git diff --cached
💡 git ls-files --stage exposes entries in the index. git diff compares the working tree with the index, while git diff --cached compares the index with HEAD.
Key Takeaways
  • The index is Git's staging area.
  • git add updates the index.
  • The next commit is constructed from the staged index state.
Knowledge Check

What does `git add` primarily update?

Objects, Packfiles, and Storage

Git can store loose objects individually, but repositories commonly use packfiles to efficiently store and transfer many objects. Git can delta-compress related content to reduce storage requirements.

git gc performs repository maintenance, including housekeeping and packing objects when appropriate.

Repository Maintenance
git count-objects -v
git gc
git verify-pack -v .git/objects/pack/*.idx
💡 These commands provide information about object storage and perform or inspect repository packing. Advanced storage commands should be used carefully.
Key Takeaways
  • Packfiles improve object storage and transfer efficiency.
  • Git can delta-compress related objects.
  • Repository maintenance is normally handled automatically or through standard Git maintenance commands.
Knowledge Check

What is a major purpose of Git packfiles?