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 cat-file -t HEAD
git cat-file -p HEAD
git cat-file -t HEAD^{tree}
git rev-parse HEADcat-file can inspect Git objects. rev-parse resolves references such as HEAD to their object IDs.- 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.
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.
git show-ref
git rev-parse HEAD
git symbolic-ref HEAD
git branch --show-current- Branches and tags are references.
- HEAD normally points symbolically to the current branch.
- References make branch creation and movement inexpensive.
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.
git add src/app.js
git ls-files --stage
git diff
git diff --cachedgit 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.- The index is Git's staging area.
git addupdates the index.- The next commit is constructed from the staged index state.
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.
git count-objects -v
git gc
git verify-pack -v .git/objects/pack/*.idx- 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.
What is a major purpose of Git packfiles?