특정 파일만 되돌리기

이럴 때 참고하세요

“전체 말고 특정 파일만 이전 버전으로 되돌리고 싶어요”

상황별 빠른 해결

수정 취소 (마지막 커밋 상태로)

git restore 파일명

특정 커밋의 파일 가져오기

git restore --source 커밋ID 파일명

다른 브랜치의 파일 가져오기

git restore --source 브랜치명 파일명

상세 가이드

1. 현재 수정 취소

아직 커밋하지 않은 수정을 취소:

# 특정 파일
git restore app.js
 
# 여러 파일
git restore app.js utils.js
 
# 특정 폴더
git restore src/
 
# 모든 파일 (⚠️ 주의!)
git restore .

복구 불가

커밋하지 않은 변경은 복구할 수 없습니다.

2. 특정 커밋에서 파일 가져오기

# 커밋 히스토리 확인
$ git log --oneline
a1b2c3d (HEAD) 현재 커밋
9x8y7z6 이전 커밋 여기의 app.js를 가져오고 싶음
5w4v3u2 이전
 
# 해당 커밋의 파일 가져오기
git restore --source 9x8y7z6 app.js
 
# 또는 HEAD 기준으로
git restore --source HEAD~1 app.js
git restore --source HEAD~2 app.js

3. 다른 브랜치의 파일 가져오기

# 다른 브랜치의 최신 파일
git restore --source main app.js
 
# 다른 브랜치의 특정 경로
git restore --source feature/login src/auth/

4. 삭제한 파일 복구

# 파일 삭제함
rm important.js
 
# 복구
git restore important.js

5. Stage에서 특정 파일만 내리기

# add한 것 취소 (변경은 유지)
git restore --staged app.js
 
# 모든 파일
git restore --staged .

과거 버전 확인 후 가져오기

파일 히스토리 확인

# 특정 파일의 변경 히스토리
git log --oneline -- app.js
 
# 각 버전의 내용 비교
git diff HEAD~1 -- app.js
git diff a1b2c3d -- app.js

특정 버전 내용 보기 (가져오기 전 확인)

# 해당 커밋의 파일 내용 보기
git show a1b2c3d:app.js
 
# 파일로 저장해서 비교
git show a1b2c3d:app.js > app.js.old

checkout 사용 (기존 방식)

restore가 없는 구버전 Git에서:

# 수정 취소
git checkout -- app.js
 
# 특정 커밋에서 가져오기
git checkout a1b2c3d -- app.js
 
# 다른 브랜치에서 가져오기
git checkout main -- app.js

자주 하는 실수

경로를 잘못 지정

# 파일이 폴더 안에 있다면 전체 경로
git restore src/components/app.js
 
# 또는 해당 폴더로 이동
cd src/components
git restore app.js

restore vs reset 혼동

# restore: 파일 단위 복원
git restore app.js
 
# reset: 커밋 히스토리 변경
git reset HEAD~1

원하지 않는 버전 가져옴

# 가져온 후 되돌리기
git restore app.js  # 현재 HEAD로 복원
 
# 또는 add 전이라면
git checkout HEAD -- app.js

실용적인 예시

실수로 수정한 설정 파일 복원

git restore config.json

어제 버전의 파일 가져오기

# 로그에서 어제 커밋 찾기
git log --oneline --since="1 day ago"
 
# 해당 커밋의 파일 가져오기
git restore --source 커밋ID app.js

삭제했다가 복구

# 파일 삭제
rm -rf src/old-feature/
 
# 복구
git restore src/old-feature/

관련 문서

더 알아보기