본문 바로가기
C.W.K.
Stream
Lesson 02 of 04 · published

Ref, HEAD, index file

~20 min · refs, head, index

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

Ref, HEAD, index가 object를 일상 명령과 이어

Object가 내용이라면 ref는 이름이야. Commit에 main이나 v2.4.0처럼 사람이 읽는 이름이 붙는 까닭은 .git/ 아래 refs/ 계층의 작은 hash 파일과 오래된 entry를 모은 packed-refs 덕분이야. HEAD는 ref를 가리키거나 detached 상태에서는 commit을 직접 가리키는 특별한 pointer야. ref, HEAD, index가 object database와 일상 명령 사이의 다리가 돼.

Ref는 세 하위 tree에 살아. refs/heads/<name>은 로컬 branch, refs/remotes/<remote>/<branch>는 마지막 fetch 때 본 remote의 읽기 전용 그림자인 remote-tracking branch, refs/tags/<name>은 tag야. Lightweight tag는 hash를 직접 가리키고 annotated tag는 tag object를 가리켜. git for-each-ref로 모두 살펴볼 수 있어.

HEAD는 Git이 모든 연산에서 수정하는 유일한 pointer야. 보통 내용은 ref: refs/heads/main처럼 branch를 가리키는 symbolic ref지. commit하면 Git이 commit object와 branch ref를 새 commit으로 갱신하고 HEAD는 같은 branch를 가리킨 채 새 tip에 있게 돼. branch를 switch하면 HEAD의 symbolic ref가 바뀌고 특정 commit을 checkout하면 HEAD가 hash를 직접 담아 detached HEAD가 돼.

세 번째 구조인 index는 .git/index의 binary file로 다음 commit의 tree 모양을 묘사해. 추적하는 path와 mode, blob hash, timestamp, 진행 중 merge의 stage 정보가 들어 있어. git ls-files --stage로 읽을 수 있어. git diff는 작업 트리와 index를, git diff --staged는 index와 HEAD tree를 비교해. Index가 있기 때문에 git add로 commit할 조각을 고를 수 있어.

Code

Ref, HEAD, index 검사·bash
# 모든 ref 한 눈에:
git for-each-ref --format='%(refname:short) -> %(objectname:short)'

# HEAD 가 가리키는 거:
cat .git/HEAD
# 보통: ref: refs/heads/main

# 현재 branch 가 가리키는 거:
cat .git/refs/heads/main

# 또는 plumbing 으로:
git symbolic-ref HEAD                  # refs/heads/main 반환
git rev-parse HEAD                     # commit hash 반환

# Index (다음 commit blueprint) 보기:
git ls-files --stage | head -10
Plumbing 레벨에서 ref 조작·bash
# Branch ref 를 다른 commit 으로 (저수준):
git update-ref refs/heads/feature-x abc1234

# Ref 값 읽기:
git rev-parse refs/heads/feature-x

# Ref 삭제:
git update-ref -d refs/heads/feature-x

# Packed refs 파일 (옛 ref 가 효율 위해 함께 저장):
cat .git/packed-refs | head

External links

Exercise

실제 repo에서 find .git/refs -type f.git/refs/를 살펴봐. ref file 하나를 cat해 hash가 git rev-parse refs/heads/<name>과 같은지 확인해. cat .git/HEAD로 branch 위인지 detached인지 구분하고 git ls-files --stage의 세 줄을 읽어. commit 하나를 만들 때 HEAD와 해당 branch ref에 무슨 일이 생기는지 적어봐.

Progress

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

댓글 0

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

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