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

세 영역: 작업 트리, index, repository

~22 min · staging, index, workflow

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

세 영역에는 각자 맡은 일이 있어

Git에서 생기는 혼란 대부분은 세 영역을 한데 뭉뚱그리는 데서 시작해. 이것만 정확히 잡아도 몇 년치 시행착오를 아낄 수 있어. 셋은 작업 트리(working tree), index(staging area), repository야.

작업 트리는 네가 디스크에서 직접 편집하는 파일들이야. 현재 checkout된 모습이 펼쳐진 곳이지. 편집기도, 빌드 도구도, 네 눈도 이 영역을 상대해.

index는 처음 배울 때 가장 낯선 영역이야. .git/index라는 binary file에 살면서 '다음 commit이 될 후보 snapshot'을 들고 있어. git add file.js는 추상적인 목록에 commit 표시를 붙이는 명령이 아니야. 그 순간의 file.js 내용을 blob으로 저장하고 index entry를 갱신해. add한 뒤 파일을 다시 바꿨다면 index에는 add 당시 version이 그대로 남아 있어. 그래서 git diff는 작업 트리와 index의 차이를, git diff --staged는 index와 마지막 commit의 차이를 보여줘.

repository.git/ 아래의 모든 것이야. object, ref, index file, hook, config, 압축된 이력이 전부 여기에 있어. git commit은 현재 index를 tree object로 얼리고 metadata를 붙여 commit object를 만든 다음, 현재 branch가 그 commit을 가리키게 해. 세 영역을 세 이동으로 기억하면 돼. git add는 작업 트리에서 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

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

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