今日已更新 328 条资讯 | 累计 20300 条内容
关于我们

How Git Actually Stores Your Code: Blobs, Trees, and Commits

pickuma 2026年06月04日 14:56 7 次阅读 来源:Dev.to

Most people picture Git as a tool that records changes — a stack of diffs layered on top of each other. That mental model is wrong, and it makes Git feel mysterious. Git is really a small key-value database that stores snapshots, and once you see the four object types it uses, commands like reset , checkout , and rebase stop being magic. Git is a content-addressed object store Everything Git tracks lives in .git/objects as an object, and every object has an ID that is the hash of its own content. By default that hash is a 40-character SHA-1 digest (newer Git supports SHA-256). The same bytes always produce the same ID, so the ID is the content's address — change one byte and you get a completely different object. This is why Git data is effectively immutable: you never edit an object in place, you create a new one with a new name. You can look inside any object with git cat-file . The -t flag prints the type, -p pretty-prints the content: $ git cat-file -t 3b18e512 blob $ git cat-file -p 3b18e512 hello world There are exactly four object types: blob , tree , commit , and tag . A blob is just file contents — raw bytes, with no filename and no metadata. The blob for README.md knows nothing about being named README.md ; it only knows what's inside. A tree is a directory listing. It maps names to other objects: each entry has a mode (like a file vs. an executable vs. a subdirectory), a name, and the hash of either a blob (a file) or another tree (a subdirectory). Trees are how Git represents folder structure. Inspecting one shows exactly that: $ git cat-file -p HEAD^ { tree } 100644 blob a906cb... README.md 040000 tree fe8e3b... src A commit ties it together. A commit object points to exactly one top-level tree (the full state of your project at that moment), plus the hash of its parent commit (or parents, for a merge), the author and committer with timestamps, and the commit message. Running git cat-file -p HEAD shows these fields in plain text. Because each commit nam

本文内容来源于互联网,版权归原作者所有
查看原文