Stage와 Working Directory

한 줄 요약

Git의 세 가지 작업 영역: Working Directory → Stage → Repository

개요

Git은 파일을 관리하기 위해 세 가지 영역을 사용합니다. 이 구조를 이해하면 Git의 동작 원리가 명확해집니다.

세 가지 영역

┌──────────────────────────────────────────────────────────────────┐
│                                                                   │
│  Working Directory        Stage              Repository          │
│  (작업 디렉토리)      (스테이징 영역)          (저장소)            │
│                                                                   │
│  ┌─────────────┐     ┌─────────────┐     ┌─────────────┐        │
│  │             │     │             │     │             │        │
│  │  파일 수정   │────▶│  변경 선택   │────▶│  영구 저장   │        │
│  │             │ add │             │commit│             │        │
│  └─────────────┘     └─────────────┘     └─────────────┘        │
│                                                                   │
└──────────────────────────────────────────────────────────────────┘

1. Working Directory (작업 디렉토리)

  • 실제로 파일을 편집하는 공간
  • 프로젝트 폴더 그 자체
  • .git 폴더를 제외한 모든 파일

2. Stage (Staging Area, Index)

  • commit할 변경사항을 준비하는 공간
  • git add로 파일을 올림
  • “이 변경사항들을 다음 commit에 포함시킬 거야”라는 의미

3. Repository (저장소)

  • commit된 변경사항이 저장되는 공간
  • .git 폴더 안에 존재
  • 모든 히스토리가 여기에 보관됨

파일의 상태 변화

                    ┌───────────────────────────────────┐
                    ▼                                   │
┌─────────┐    ┌─────────┐    ┌─────────┐    ┌─────────┴──┐
│Untracked│───▶│Unmodified│───▶│Modified │───▶│   Staged   │
│ (미추적) │add │ (변경없음)│edit│ (수정됨) │add │(스테이지됨)│
└─────────┘    └─────────┘    └─────────┘    └────────────┘
                    ▲                              │
                    │            commit            │
                    └──────────────────────────────┘
상태설명
UntrackedGit이 추적하지 않는 새 파일
Unmodified마지막 commit 이후 변경 없음
Modified파일이 수정되었지만 stage 안 됨
Staged다음 commit에 포함될 예정

왜 Stage가 필요할까?

Stage 영역이 있어서 선택적 commit이 가능합니다.

예시 상황

# 3개 파일을 수정했지만, 2개만 commit하고 싶을 때
 
# 수정한 파일들
modified: login.js      # 로그인 기능 수정 ✓
modified: style.css     # 스타일 수정 ✓
modified: debug.js      # 테스트용 코드 (commit 안 할 것)
 
# 원하는 파일만 stage
git add login.js style.css
 
# commit (debug.js는 포함 안 됨)
git commit -m "feat: 로그인 기능 및 스타일 수정"

Stage의 장점

  1. 관련 있는 변경만 묶어서 commit 가능
  2. 실험적인 코드를 제외하고 commit 가능
  3. commit 전에 검토 기회 제공

상태 확인하기

git status로 현재 상태를 확인할 수 있습니다:

$ git status
 
On branch main
Changes to be committed:                    # Stage에 있음
  (use "git restore --staged <file>..." to unstage)
        modified:   login.js
 
Changes not staged for commit:              # 수정했지만 Stage 아님
  (use "git add <file>..." to update what will be committed)
        modified:   style.css
 
Untracked files:                            # 새 파일 (추적 안 함)
  (use "git add <file>..." to include in what will be committed)
        newfile.txt

영역 간 이동 명령어

Working Directory ──git add──▶ Stage ──git commit──▶ Repository
                                 │
        ◀──git restore──────────┘
        ◀──git restore --staged──┘
이동명령어
Working → Stagegit add 파일
Stage → Repositorygit commit
Stage → Working (취소)git restore --staged 파일
Working 변경 취소git restore 파일

실제 작업 흐름 예시

# 1. 파일 수정 (Working Directory에서 작업)
echo "새 코드" >> app.js
 
# 2. 상태 확인
git status
# modified: app.js (빨간색 - unstaged)
 
# 3. Stage에 올리기
git add app.js
 
# 4. 상태 다시 확인
git status
# modified: app.js (초록색 - staged)
 
# 5. Commit (Repository에 저장)
git commit -m "feat: 새 기능 추가"
 
# 6. 상태 확인
git status
# nothing to commit, working tree clean

자주 하는 실수

잘못된 파일을 stage에 올렸을 때

# stage에서 내리기 (파일 내용은 유지)
git restore --staged 잘못올린파일.js

수정을 취소하고 싶을 때

# ⚠️ 주의: 변경사항이 사라집니다!
git restore 파일명

git restore 참고

관련 명령어

더 알아보기