Git Handbook

09 · Git Internals

Blob, tree, commit, refs, HEAD, object database, plumbing commands và revision syntax.

1. Git object model

Git lưu dữ liệu theo content-addressed objects. Bốn object bạn cần biết:

ObjectVai trò
blobNội dung file, không chứa filename.
treeMapping tên → blob/tree cùng mode.
commitTrỏ tới tree + parent(s) + metadata + message.
tagAnnotated tag object có metadata, thường trỏ commit.
commit
  └── tree
      ├── README.md -> blob
      ├── package.json -> blob
      └── src -> tree
          ├── app.js -> blob
          └── auth.js -> blob

Pro Git: Git Objects ↗

2. Refs, HEAD và commit graph

Branch/tag thường được biểu diễn qua refs. HEAD thường symbolic-ref tới branch hiện tại.

cat .git/HEAD
# ref: refs/heads/main

cat .git/refs/heads/main
# <object-id>
Đừng chỉnh trực tiếp file trong .git khi chưa hiểu rõ; hãy dùng command porcelain/plumbing tương ứng.

3. Plumbing commands

git-cat-file

Inspect type/content/size của object Git.
Official docs ↗
Cú pháp
git cat-file -t|-p|-s <object>
Hiểu đơn giản
Công cụ nền tảng để khám phá internals.
git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -p HEAD^{tree}

git-rev-parse

Parse revision và query thông tin repository.
Official docs ↗
Cú pháp
git rev-parse <rev>|--show-toplevel
Hiểu đơn giản
Hữu ích cả khi học revision syntax và viết shell scripts.
git rev-parse HEAD
git rev-parse HEAD~3
git rev-parse --show-toplevel
git rev-parse --is-inside-work-tree

git-ls-tree

Liệt kê entries trong tree object.
Official docs ↗
Cú pháp
git ls-tree <tree-ish>
Hiểu đơn giản
Cho thấy mode, type, object ID và path.
git ls-tree HEAD
git ls-tree -r HEAD

git-hash-object

Tính object ID và tùy chọn ghi object vào object database.
Official docs ↗
Cú pháp
git hash-object [-w] <file>
Hiểu đơn giản
Dùng để hiểu content-addressing; hiếm khi cần trong daily workflow.
echo "hello" > /tmp/hello.txt
git hash-object /tmp/hello.txt
git hash-object -w /tmp/hello.txt

4. Integrity & maintenance

git-fsck

Kiểm tra connectivity/validity của object database.
Official docs ↗
Cú pháp
git fsck [options]
Hiểu đơn giản
Hữu ích khi chẩn đoán corruption hoặc tìm dangling objects.
git fsck
git fsck --lost-found

git-gc

Garbage collection và optimization repository.
Official docs ↗
Cú pháp
git gc [options]
Hiểu đơn giản
Git thường tự maintenance; không cần chạy thường xuyên bằng tay.
git gc

5. Revision syntax

SyntaxÝ nghĩa
HEADcurrent HEAD
HEAD~1first-parent 1 generation trước
HEAD^parent thứ nhất
merge^2parent thứ hai của merge commit
main..featurecommit reachable từ feature nhưng không từ main
main...featuresymmetric difference giữa hai phía

gitrevisions docs ↗