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

commit, parent, DAG

~20 min · dag, commit-graph

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

commit은 노드, branch는 pointer

commit이 snapshot이라는 걸 받아들였다면 이제 snapshot들이 어떻게 연결되는지 볼 차례야. 첫 commit을 뺀 모든 commit은 parent의 hash를 기록하고, merge commit은 parent를 둘 가져. 그 결과가 Directed Acyclic Graph, 줄여서 DAG야. 연결은 과거를 향한 한 방향이고(directed), 따라가도 출발점으로 되돌아오지 않으며(acyclic), 나란히 갈라진 branch와 merge가 선이 아닌 진짜 graph를 만들어(graph).

직접 그려봐. A ← B ← C ← Dmain의 직선 이력이라고 하자. B에서 feature가 갈라져 E ← F를 더하면 graph가 두 갈래가 돼. featuremain에 merge하면 새 G가 생기고, 그 parent는 DF야. 이제 G에서 어느 parent를 따라가도 과거 작업에 닿을 수 있어. 지워지거나 덮어써지는 게 아니라 graph가 자라는 거야.

branch와 tag는 hash 하나를 담은 41 byte 파일이야. feature를 만든다는 건 파일 하나를 쓰는 일이고, switch하면 HEAD가 그 파일을 가리켜. branch를 지우면 pointer는 사라지지만 commit은 남아 있어. 다른 ref도 그 commit을 가리키지 않게 되면 결국 Git GC가 정리해. branch가 값싼 까닭은 복사할 것이 거의 없기 때문이야.

log, blame, bisect, rebase, cherry-pick 같은 강력한 Git 명령은 모두 graph 연산이야. 명령을 graph 지시문으로 읽기 시작하면 이름도 덜 자의적으로 보여. git log feature..main은 'main에서는 도달할 수 있지만 feature에서는 도달할 수 없는 commit들'을 뜻해. DAG 위의 집합 차이인 셈이지.

Code

repo의 실제 DAG 그려 보기·bash
# 모든 branch 의 ASCII graph
git log --oneline --graph --decorate --all

# 최근 20 노드, author + 날짜 포함
git log --oneline --graph --decorate --all --pretty='%h %ad %an %d %s' --date=short -n 20

# commit 하나의 parent 보기
git cat-file -p HEAD | head -3
branch와 tag는 폴더가 아니라 pointer야·bash
# main 이 실제로 가리키는 건?
cat .git/refs/heads/main

# HEAD 는?
cat .git/HEAD

# ref 한꺼번에 보기
git for-each-ref --sort=committerdate \
  --format='%(refname:short) -> %(objectname:short)'

External links

Exercise

연습용 repo의 main에 commit 네 개를 만들고, feature로 분기해 commit 두 개를 더해. main으로 돌아와 commit 하나를 더 만든 뒤 feature를 merge해. git log --oneline --graph --decorate --all을 실행하고 parent가 둘인 commit을 짚어봐. 이어서 git log main..feature가 이 graph에서 무엇을 뜻하는지 한 줄로 설명해.

Progress

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

댓글 0

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

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