[GitHub 100일 챌린지] Day 27 - 파일 상태 4가지 이해하기
[GitHub 100일 챌린지] Day 27 - 파일 상태 4가지 이해하기
100일 챌린지 Day 27 - Git에서 파일이 가질 수 있는 4가지 상태를 배웁니다
배울 내용
- Untracked, Unmodified, Modified, Staged
- 상태 간 전환 과정
- git status로 상태 확인
Topic1. 파일 상태 4가지
Git 파일은 4가지 상태 중 하나입니다.
1. Untracked (추적 안 됨)
정의: Git이 아직 모르는 새 파일
1
2
3
# 새 파일 생성
echo "New" > new-file.txt
git status
출력:
1
2
Untracked files:
new-file.txt
특징:
1
2
3
🔴 Git이 추적하지 않음
🔴 커밋 불가
🔴 git add 필요
2. Unmodified (수정 안 됨)
정의: 커밋된 후 변경 없는 파일
1
2
git commit -m "Add file"
git status
출력:
1
nothing to commit, working tree clean
특징:
1
2
3
✅ 최신 커밋과 동일
✅ 안정 상태
✅ git status에 안 보임
3. Modified (수정됨)
정의: 커밋 이후 수정된 파일
1
2
3
# 파일 수정
echo "Modified" >> file.txt
git status
출력:
1
2
Changes not staged for commit:
modified: file.txt
특징:
1
2
3
🟡 Working Directory에만 변경
🟡 아직 스테이징 안 됨
🟡 git add 필요
4. Staged (스테이징됨)
정의: 커밋 준비 완료
1
2
git add file.txt
git status
출력:
1
2
Changes to be committed:
modified: file.txt
특징:
1
2
3
🟢 커밋 준비 완료
🟢 다음 커밋에 포함
🟢 git commit 가능
결과: 파일의 4가지 상태를 이해합니다
Topic2. 상태 간 전환
파일 상태가 어떻게 변하는지 배웁니다.
상태 다이어그램
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
┌─────────────┐
│ Untracked │ 새 파일
└─────────────┘
│ git add
↓
┌─────────────┐
│ Staged │ 커밋 준비
└─────────────┘
│ git commit
↓
┌─────────────┐
│ Unmodified │ 안정 상태
└─────────────┘
│ 파일 수정
↓
┌─────────────┐
│ Modified │ 수정됨
└─────────────┘
│ git add
↓
┌─────────────┐
│ Staged │ 커밋 준비
└─────────────┘
전체 라이프사이클
해보기:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1. Untracked
echo "Hello" > demo.txt
git status # Untracked
# 2. Staged
git add demo.txt
git status # Staged
# 3. Unmodified (커밋 후)
git commit -m "Add demo"
git status # Clean (Unmodified)
# 4. Modified
echo "World" >> demo.txt
git status # Modified
# 5. Staged
git add demo.txt
git status # Staged
# 6. Unmodified (커밋 후)
git commit -m "Update demo"
git status # Clean
상태 되돌리기
Staged → Modified:
1
git restore --staged file.txt
Modified → Unmodified:
1
git restore file.txt # ⚠️ 변경사항 사라짐!
Untracked 제거:
1
2
rm file.txt # 또는
git clean -f
결과: 파일 상태를 자유롭게 전환할 수 있습니다
Topic3. git status로 상태 확인
git status를 제대로 읽는 방법입니다.
git status 출력 이해
해보기:
1
2
3
4
5
6
# 다양한 상태 만들기
echo "New" > new.txt # Untracked
echo "Change" >> existing.txt # Modified
git add existing.txt # Staged
git status
출력:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: existing.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes)
modified: another.txt
Untracked files:
(use "git add <file>..." to include)
new.txt
섹션별 의미
Changes to be committed (Staged):
1
2
🟢 다음 커밋에 포함될 파일
→ git commit 가능
Changes not staged (Modified):
1
2
🟡 수정되었지만 스테이징 안 됨
→ git add 필요
Untracked files:
1
2
🔴 Git이 모르는 새 파일
→ git add 필요
단축 출력
간단히 보기:
1
2
3
git status -s
# 또는
git status --short
출력 예시:
1
2
3
4
5
A new-file.txt # Added (Staged)
M modified.txt # Modified (Staged)
M another.txt # Modified (Not staged)
MM both.txt # Modified (Both)
?? untracked.txt # Untracked
기호 의미:
1
2
3
4
5
6
?? Untracked
A Added (Staged)
M Modified
MM Modified (Staged + Working)
D Deleted
R Renamed
해보기: 종합 실습
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. 여러 상태 만들기
echo "A" > staged.txt
git add staged.txt
echo "B" > modified.txt
git add modified.txt
git commit -m "Add files"
echo "Changed" >> modified.txt
echo "C" > untracked.txt
# 2. 상태 확인
git status
git status -s
# 3. 각 파일 상태 파악
결과: git status 출력을 정확히 이해하고 파일 상태를 파악할 수 있습니다
정리
완료 체크:
- 파일 상태 4가지를 설명할 수 있다
- 상태 간 전환 과정을 이해했다
- git status를 읽을 수 있다
4가지 상태:
1
2
3
4
🔴 Untracked: 새 파일
✅ Unmodified: 변경 없음
🟡 Modified: 수정됨
🟢 Staged: 커밋 준비
상태 전환:
1
2
3
4
Untracked → git add → Staged
Staged → git commit → Unmodified
Unmodified → 수정 → Modified
Modified → git add → Staged
다음: Day 28 - git add로 스테이징 →
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.
