C.W.K.
Stream
Lesson 01 of 04 · published

Object: blob, tree, commit, tag

~22 min · objects, internals

Level 0Untracked 새싹
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Object 타입 넷과 content-addressable store

Git 이 아는 모든 것이 .git/objects/ 의 네 object 타입 중 하나로 저장, 내용의 SHA-1 hash 로 주소지정. blob, tree, commit, tag 를 repo 에서 식별할 수 있게 되면 상위 Git 명령이 마법 같지 않아져 — 다 이 네 모양의 조작.

Blob 은 파일 하나의 raw bytes 저장. 파일명 없음, permission 없음 — 그냥 내용. 같은 내용 파일 둘 (중복 config, 두 test suite 의 같은 fixture) 은 같은 blob 으로 hash 되고 한 번 저장. Tree 는 디렉토리 listing 저장: 이름, mode (파일 permission + 타입), blob (파일) 또는 다른 tree (서브디렉토리) 의 hash. Tree 가 blob 에 이름과 구조 부여.

Commit 이 다 묶음: root tree hash (프로젝트 스냅샷), parent commit hash, author + committer + timestamp, 메시지 가진 작은 object. Commit parent 따라가면 history 거꾸로, commit tree 읽으면 옆으로 '이 시점 모든 게 어땠나' 진입. Tag 는 더 드문 네 번째 타입: metadata 있는 명명 pointer, annotated tag 에 사용. Lightweight tag 는 그냥 ref, tag object 아님.

Store 자체가 content-addressable. echo "hello" | git hash-object --stdin 이 hash 계산, git cat-file -p <hash> 가 어떤 object 든 다시 읽음. Hash 의 첫 두 글자가 .git/objects/ 아래 디렉토리 이름, 나머지 38 글자가 파일명 — 그래서 초기 Git 검사가 .git/objects/3a/4f5d6e... 같은 파일 보여줌. Repo 커지면 Git 이 많은 object 를 효율 위해 pack 파일로 압축 (다음 레슨). Mental model 은 압축 후에도 살아남: hash → object → content.

Code

Repo 의 object 타입 투어·bash
# 최신 commit:
HASH=$(git rev-parse HEAD)

# Commit object 읽기:
git cat-file -p $HASH
# 출력에 tree, parent, author, committer, 메시지 표시.

# Commit 이 가리키는 root tree 읽기:
TREE=$(git cat-file -p $HASH | head -1 | awk '{print $2}')
git cat-file -p $TREE
# 각 줄: <mode> <type> <hash>\t<name>

# Tree 에서 blob hash 골라 읽기:
git cat-file -p <blob-hash>

# Hash 의 타입은?
git cat-file -t $HASH        # commit
git cat-file -t $TREE        # tree
Object 수동 계산 + 저장·bash
# 저장 없이 hash 계산:
echo "hello world" | git hash-object --stdin

# Blob 으로 .git/objects/ 에 계산 + 저장:
echo "hello world" | git hash-object --stdin -w

# Hash 로 다시 읽기:
git cat-file -p <hash>

# HEAD tree 의 모든 unique blob hash list:
git ls-tree -r HEAD | awk '{print $3, $4}'

External links

Exercise

아무 작은 repo 에서 git rev-parse HEAD 로 commit hash, 체인 수동 walk: git cat-file -p 를 commit 에, 그 다음 tree 에, tree 의 blob 하나에. git ls-tree -r HEAD | wc -l 로 현재 스냅샷의 blob 카운트. Training 시절 mental model 에서 잘못 이해했을지도 모를 Git 저장에 대해 확인한 사실 둘 적어.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.