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

restore: 작업 트리와 staging 되돌리기

~18 min · restore, undo

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

restore는 파일을 정밀하게 되돌려

오랫동안 Git에서 파일을 되돌리는 명령은 git checkout -- file이었어. branch를 바꾸는 checkout과 역할이 겹쳐 입문자를 계속 헷갈리게 했지. Git 2.23이 도입한 git restore는 branch를 바꾸는 checkout과 파일 복원을 분리해 세 영역 사이에서 파일 내용을 옮기는 일만 맡아. 뜻이 훨씬 선명해.

세 가지가 거의 모든 경우를 다뤄. stage하지 않은 편집 버리기: git restore file.js는 작업 트리의 version을 index의 version으로 덮어써 로컬 편집을 없애. Unstage: git restore --staged file.js는 index entry를 HEAD version으로 되돌려 다음 commit에서는 빼되 작업 트리의 편집은 남겨. 둘 다 되돌리기: git restore --staged --worktree file.js는 index와 작업 트리를 HEAD에 맞춰 이 파일의 최근 작업을 모두 없애.

--source를 쓰면 다른 commit에서 복원할 수도 있어. git restore --source=HEAD~3 -- file.js는 세 commit 전의 file.js를 작업 트리로 가져와. 필요하면 index까지 함께 바꿀 수 있어. 프로젝트 전체를 움직이지 않고 파일 하나만 지난주 모습으로 되돌리는 방법이야.

Restore는 조용하고 좁은 undo야. branch pointer도 옮기지 않고 이력도 다시 쓰지 않으며 다른 파일을 건드리지 않아. 폭발 반경은 명시한 path 그대로지. 파일 하나의 실수에는 branch 전체와 commit하지 않은 변경을 함께 덮는 git reset --hard보다 훨씬 알맞아.

Code

Restore 세 가지 맛·bash
# 한 파일의 unstaged 편집 폐기:
git restore src/app.js

# 파일 unstage (편집 유지, 다음 commit 에서 빼기):
git restore --staged src/app.js

# 이 파일의 index + working tree 를 HEAD 로 rewind:
git restore --staged --worktree src/app.js

# 옛 commit 에서 복원:
git restore --source=HEAD~3 -- src/app.js
Interactive restore 는 hunk 단위·bash
# Hunk 순회하며 어느 걸 폐기할지 고르기:
git restore -p src/app.js

# Hunk 순회하며 어느 걸 unstage 할지 고르기:
git restore --staged -p src/app.js

# unstaged 변경 있는 모든 거 복원 (파괴적):
git restore .

External links

Exercise

프로젝트 파일 하나에 분명히 나쁜 변경을 만들어. git restore -p <file>로 나쁜 hunk만 버리고 다른 편집은 남겨. 또 다른 경우에는 git add한 변경이 다음 commit에 어울리지 않는다고 판단하고 git restore --staged <file>로 작업 트리의 편집을 유지한 채 unstage해. 매번 git statusgit diff로 결과를 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

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

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