Git은 버전 관리 시스템이므로 언제든 이전 상태로 되돌릴 수 있습니다.
상황에 맞게 선택:
git restore → 파일 하나 복구git revert → 특정 commit 취소git reset → 특정 commit 시점으로 시간 되돌리기git switch / git checkout → 다른 branch나 commit으로 이동# 최근 commit 상태로 되돌리기
git restore 파일명
# 특정 commit 시점으로 복구
git restore --source 커밋아이디 파일명
# staging 취소
git restore --staged 파일명
# 특정 commit 취소
git revert 커밋아이디
# 여러 commit 한 번에 취소
git revert 커밋아이디1 커밋아이디2
# 가장 최근 commit 취소
git revert HEAD
merge commit도 revert 가능 (merge 취소 효과).
실행 시 Vim 에디터가 열리면:
i → 메시지 수정esc → :wq 입력 후 종료# 완전히 과거 시점으로 되돌림 (작업폴더도 reset)
git reset --hard 커밋아이디
# staging area 유지
git reset --soft 커밋아이디
# staging 해제 (작업 내용은 유지)
git reset --mixed 커밋아이디
# 특정 commit 시점으로 이동 (detached HEAD 상태)
git checkout 커밋아이디
# or 최신 방식
git switch --detach 커밋아이디
# 다시 main 브랜치로 돌아오기
git switch main
detached HEAD 상태에서 수정 후 commit 하면, 임시 branch처럼 동작하므로 필요 시 새 branch 생성:git switch -c 새브랜치명
git reset은 협업 저장소에서 금지untracked 파일(git add 안 한 파일)은 reset으로 삭제되지 않음git clean -fd