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:
| Object | Vai trò |
|---|---|
| blob | Nội dung file, không chứa filename. |
| tree | Mapping tên → blob/tree cùng mode. |
| commit | Trỏ tới tree + parent(s) + metadata + message. |
| tag | Annotated tag object có metadata, thường trỏ commit. |
commit
└── tree
├── README.md -> blob
├── package.json -> blob
└── src -> tree
├── app.js -> blob
└── auth.js -> blob2. 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.
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.
git rev-parse HEAD
git rev-parse HEAD~3
git rev-parse --show-toplevel
git rev-parse --is-inside-work-treegit-ls-tree
Liệt kê entries trong tree object.
git ls-tree HEAD
git ls-tree -r HEADgit-hash-object
Tính object ID và tùy chọn ghi object vào object database.
echo "hello" > /tmp/hello.txt
git hash-object /tmp/hello.txt
git hash-object -w /tmp/hello.txt4. Integrity & maintenance
git-fsck
Kiểm tra connectivity/validity của object database.
git fsck
git fsck --lost-foundgit-gc
Garbage collection và optimization repository.
git gc5. Revision syntax
| Syntax | Ý nghĩa |
|---|---|
HEAD | current HEAD |
HEAD~1 | first-parent 1 generation trước |
HEAD^ | parent thứ nhất |
merge^2 | parent thứ hai của merge commit |
main..feature | commit reachable từ feature nhưng không từ main |
main...feature | symmetric difference giữa hai phía |