Sub topic 2
Blobs, Trees, Commits, and Refs
Assume you know init, add, commit, and log, can use a Unix-style shell, and understand that a hash summarizes data.
Blobs: file contents without names
A blob is a Git object that stores file content. It does not store the file name, path, or permissions. Git gives the content an object ID, normally a SHA-1 hash.
mkdir plumbing-demo
cd plumbing-demo
git init -q
blob=$(printf 'hello\n' | git hash-object -w --stdin)
echo "$blob"
git cat-file -t "$blob"
git cat-file -s "$blob"
git cat-file -p "$blob"-w writes the blob to .git/objects. The same content produces the same object ID in the same repository. git cat-file reads an object: -t shows its type, -s its size, and -p its content.
Trees: directories, names, and modes
A tree records directory entries. Each entry contains a mode, object type, object ID, and name. For a regular file, mode 100644 means a normal non-executable file.
git update-index --add --cacheinfo 100644 "$blob" hello.txt
tree=$(git write-tree)
git cat-file -p "$tree"
git ls-tree "$tree"The index is Git’s staging data structure. write-tree converts its entries into a tree object. A tree can point to blobs for files or other trees for subdirectories.
Commits: snapshots, parents, and metadata
A commit stores a pointer to a root tree, zero or more parent commit IDs, author and committer metadata, and a message.
commit=$(printf 'first commit\n' | git -c user.name=Demo -c [email protected] commit-tree "$tree")
git cat-file -p "$commit"A later commit points to its predecessor with a parent field. This creates history while each commit identifies a complete snapshot through its tree.
Refs and HEAD: names for commits
A ref is a mutable name that stores an object ID. A branch such as refs/heads/main usually names a commit. HEAD normally points symbolically to the current branch.
git update-ref refs/heads/main "$commit"
git symbolic-ref HEAD refs/heads/main
git rev-parse HEAD
git rev-parse HEAD^{tree}Moving the branch changes the name’s target; it does not rewrite the commit object.
Exercise
Where is the name hello.txt stored: in the blob, tree, or commit?
Show answer
In the tree. The blob stores only the file content, while the commit points to the tree.