C.W.K.
Stream
Lesson 01 of 05 · published

restore: working tree와 staging 되돌리기

~18 min · restore, undo

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

restore 는 파일용 정밀 undo

수년간 Git 에서 '파일 undo' 는 git checkout -- file 이었어, 이건 branch switch 와 overload 돼 입문자를 끊임없이 헷갈리게 했어. Git 2.23 이 git restore 도입 — 세 영역 사이 파일 내용 이동만 하는 명령. 한 번 쓰기 시작하면 옛 file 용 checkout 호출이 사고 대기 중인 것처럼 느껴져.

Undo 세 가지가 거의 모든 걸 커버. Stage 안 한 편집 버리기: git restore file.js 가 working tree version 을 index version 으로 덮어써 — local 편집 폐기. Unstage: git restore --staged file.js 가 index entry 를 HEAD version 으로 덮어써 — 다음 commit 에서 빼되 working tree 편집 유지. 둘 다 한 번에: git restore --staged --worktree file.js 가 양쪽 layer 를 HEAD 로 rewind — 이 파일의 최근 작업 완전 제거.

선택적 --source flag 로 다른 commit 에서 복원 가능 — git restore --source=HEAD~3 -- file.js 는 3 commit 전 file.js version 을 working tree 로 (선택적으로 index 까지). 이게 '지난주 모습으로' 한 파일만 되돌리고 나머지 프로젝트는 안 건드리는 방법.

Restore 를 조용한 undo 로 다뤄. branch pointer 안 옮기고, history 안 rewrite 하고, 다른 파일 영향 0. 폭발 반경 = 명시한 path 그대로. 자유롭게 쓰기 안전 — 한 파일 실수에 종종 잘못된 선택인 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 로 stage 한 변경이 안 어울린다고 결정, git restore --staged <file> 으로 working tree 편집 유지하며 unstage. 각 결과를 git status + git diff 로 검증.

Progress

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

댓글 0

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

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