C.W.K.
Stream
Lesson 03 of 05 · published

세 구역: worktree, index, repository

~22 min · staging, index, workflow

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

세 영역, 세 가지 책임

Git 의 거의 모든 혼란은 세 영역을 헷갈리는 데서 와. 한 번만 제대로 박아두면 추측 몇 년치를 아껴. 셋은 working tree, index (staging area), repository.

working tree 는 네가 편집하는 그냥 디스크 위 파일들이야. Git 이랑 직접 상관없어 — 그냥 현재 checkout 된 상태일 뿐이야. editor 도, build tool 도, 네 눈도 다 working tree 와 상호작용해.

index (staging area) 는 가장 의외의 layer 야. .git/index 라는 binary file 에 살면서 '다음 commit 후보 snapshot' 을 들고 있어. git add file.js 는 어딘가 추상적인 list 에 'commit 표시' 하는 게 아니야 — file.js 의 그 순간 내용을 blob 으로 index 에 복사하고 entry 를 업데이트해. add 후에 file 을 또 바꿨으면? index 는 add 시점 version 을 그대로 들고 있어. 그래서 git diff 는 working tree vs index, git diff --staged 는 index vs 마지막 commit 을 보여주는 거야.

repository.git/ 아래 전부 — object, ref, index file 자체, hook, config, packed history. git commit 은 현재 index 를 가져가 tree object 로 얼리고, metadata 를 씌워서 commit object 로 만들고, 현재 branch 를 그 commit 으로 가리키게 해. 세 영역, 세 명령: git add = working tree → index, git commit = index → repository, git restore = 양방향 되돌리기.

Code

세 영역이 갈라지는 모습·bash
echo 'v1' > note.txt
git add note.txt              # index 가 v1 보유, working tree 도 v1
echo 'v2' > note.txt          # working tree v2, index 는 여전히 v1

git diff                       # working tree vs index → v1 → v2 보여줌
git diff --staged              # index vs 마지막 commit → 없음 → v1
git status -s                  # 두 flag 다 보임: index (왼쪽), worktree (오른쪽)
Restore 로 영역 사이를 되돌리기·bash
# index version 을 working tree 로 가져오기 (local 편집 버리기)
git restore note.txt

# 마지막 commit 을 index 로 (unstage)
git restore --staged note.txt

# 마지막 commit 을 working tree 까지 (둘 다 rewind)
git restore --source=HEAD --staged --worktree note.txt

External links

Exercise

작은 repo 하나에 파일 하나 만들고 version A 를 git add. add 다시 안 하고 version B 로 편집. git diff, git diff --staged, git status -s 차례로 실행. 그 다음 git commit -m 'check' 하고 git show 로 확인. 어떤 version 이 commit 에 들어갔고 왜 그런지 글로 적어.

Progress

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

댓글 0

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

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