IT/git

git error: Your local changes to the following files would be overwritten by checkout 에러 발생

generator 2024. 1. 30. 15:17

"error: Your local changes to the following files would be overwritten by checkout" 에러는 Git에서 git checkout 명령어를 실행하려고 할 때, 현재 작업 디렉토리에 저장된 변경 사항이 해당 브랜치나 커밋으로 변경되면서 손실될 것임을 경고하는 메시지다.

다음과 같은 경우 발생 할 수 있다.

  1. 변경된 파일이 있을 때: 현재 작업 디렉토리에 있는 파일이 수정되었고, 해당 파일이 현재 체크아웃하려는 브랜치나 커밋에서는 수정되지 않았을 때
  2. Uncommitted 변경 사항이 있을 때: 작업 디렉토리의 변경 사항이 아직 커밋되지 않았을 때, 즉 Staging Area에 올라가지 않은 변경 사항이 있는 경우

이 경우 stash를 사용해서 처리 할수 있다.

#1.변경 내용 확인
git status

#2. 변경 사항 커밋 또는 저장
git commit -m "Commit message" 또는  git stash

#3. 브랜치 커밋
git checkout <branch_or_commit>

#4. Stash에서 복원
git stash apply