포스트

[GitHub 100일 챌린지] Day 64 - Git Restore로 파일 복구

[GitHub 100일 챌린지] Day 64 - Git Restore로 파일 복구

100일 챌린지 Day 64 - Git 2.23 버전에 추가된 restore는 파일 복구를 위한 직관적인 명령어입니다.

배울 내용

  1. Git Restore의 개념
  2. Working Directory와 Staging Area 복구
  3. Restore vs Reset 차이

1. Git Restore란?

목적: 파일을 특정 상태로 복구

git restore는 “파일 단위로 되돌리기”에 집중한 명령어입니다. 예전에는 git checkout -- file.js처럼 checkout을 파일 복구에도 사용했지만, Git 2.23 이후에는 역할이 더 분명한 restore를 쓰는 편이 이해하기 쉽습니다.

1
2
3
4
5
# Working Directory 복구
git restore file.js

# Staging Area 복구
git restore --staged file.js

핵심은 어느 영역을 되돌리는지 구분하는 것입니다.

되돌릴 대상 명령어 파일 내용
Working Directory git restore file.js 마지막 커밋 상태로 되돌림
Staging Area git restore --staged file.js stage만 취소, 파일 내용은 유지
특정 커밋의 파일 git restore --source=커밋 file.js 지정한 커밋의 파일로 복구

2. Working Directory 복구

1
2
3
4
5
6
7
# 파일 수정 취소
echo "wrong content" > file.js
git restore file.js
# → HEAD의 file.js로 복구

# 모든 수정 취소
git restore .

3. Staging Area 복구

1
2
3
4
5
6
# 실수로 add
git add sensitive-data.txt

# Unstage
git restore --staged sensitive-data.txt
# → Staging에서 제거, 파일은 유지

--staged는 커밋 후보에서 빼는 명령입니다. 파일을 삭제하거나 내용을 되돌리지 않습니다.

1
2
3
4
git add app.js
git restore --staged app.js
git status
# app.js는 modified 상태로 남아 있음

즉 “이번 커밋에는 넣지 않겠다”는 의미입니다.


4. 특정 커밋에서 복구

1
2
3
4
5
# 특정 커밋의 파일로 복구
git restore --source=abc1234 file.js

# 또는 브랜치
git restore --source=main file.js

5. 실전 시나리오

시나리오 1: 파일 수정 취소

1
2
3
4
5
# 잘못 수정
vim config.js

# 복구
git restore config.js

시나리오 2: Staging 취소

1
2
3
4
git add file1.js file2.js file3.js
# file3.js는 빼야 함

git restore --staged file3.js

시나리오 3: 삭제된 파일 복구

1
2
3
4
rm important.js
# 앗! 실수!

git restore important.js

6. Restore vs Reset

명령어 대상 용도
restore 파일 파일 복구
reset 커밋 커밋 되돌리기
1
2
3
4
5
# 파일 복구
git restore file.js

# 커밋 되돌리기
git reset --soft HEAD~1

조금 더 실무적으로 보면 이렇게 나눌 수 있습니다.

  • 파일 하나의 수정만 취소하고 싶다 → git restore file
  • 실수로 stage한 파일만 내리고 싶다 → git restore --staged file
  • 최근 커밋 자체를 다시 정리하고 싶다 → git reset --soft HEAD~1
  • 공유된 커밋을 안전하게 되돌리고 싶다 → git revert 커밋

명령어를 고를 때는 “파일을 되돌리는가, 커밋 기록을 바꾸는가”를 먼저 생각하세요.


7. 주의사항

1
2
3
4
5
6
7
# ⚠️ 주의: 복구 불가능
git restore file.js
# → 수정 내용 영구 삭제!

# 백업 권장
cp file.js file.js.backup
git restore file.js

git restore file.js는 아직 커밋하지 않은 수정 내용을 버립니다. Git이 추적 중인 파일이라도 커밋되지 않은 변경은 복구하기 어렵습니다.

안전한 확인 순서:

1
2
3
git status --short
git diff file.js
git restore file.js

git diff로 버릴 내용을 눈으로 확인한 뒤 실행하면 실수가 줄어듭니다.


8. 실전 연습

작은 테스트 저장소에서 아래 순서로 연습해보세요.

1
2
3
4
5
6
7
8
9
10
11
12
echo "version 1" > note.txt
git add note.txt
git commit -m "Add note"

echo "broken change" > note.txt
git diff
git restore note.txt

echo "draft change" > note.txt
git add note.txt
git restore --staged note.txt
git status --short

이 연습을 마치면 “내용 되돌리기”와 “stage 취소”가 다르다는 감각이 잡힙니다.


정리

완료 체크:

  • Restore로 파일을 복구할 수 있다
  • Staged 파일을 unstage할 수 있다
  • Restore와 Reset의 차이를 안다
  • git diff로 버릴 변경을 확인한 뒤 restore를 실행할 수 있다

핵심 요약:

  • git restore file - 수정 취소
  • git restore --staged file - unstage
  • git restore --source=커밋 file - 특정 커밋에서 복구
  • restore는 파일 중심, reset은 커밋/브랜치 포인터 중심

다음: Day 65 - Git Clean


이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.