[Git] Git Merge 또는 Git checkout 오류

2022. 4. 13. 17:00Git

git pull origin master 또는 git checkout master 와 같이 브랜치를 변경하거나, 원격저장소에서 pull을 받을때

error: Your local changes to the following files would be overwritten by merge:

error: Your local changes to the following files would be overwritten by checkout:

위와 같은 오류와 함께 pull이나 checkout이 동작하지 않습니다.

에러 메세지를 자세히 보면 해결 방법이 나와있습니다.

Please commit your changes or stash them before you merge. 그리고

Please commit your changes or stash them before you switch branches. 라는 문구가 있습니다.

메세지 그대로 merge 또는 switch branch 이전에 변경사항을 commit 하거나 stash 하라고 합니다.

위에서도 말했지만 저는 쓸데 없는 커밋을 하고싶지 않아 방법을 모르고 새 프로젝트를 실행했지만

이때는 git stash 명령어를 사용하면 됩니다.

현재 Staging 영역에 있는 파일의 변경사항을 스택에 넣어둡니다. 

$ git stash

아래 명령어와 같이 원격 저장소의 master에서 pull을 하거나, git checkout master와 같이 브랜치를 바꿀 수 있습니다. 

git pull origin master

stash 명령어로 스택에 넣어둔 변경 사항을 적용하고, 스택에서 제거하여줍니다.

git stash pop

간단하게 한줄로 표현하면

git stash && git pull origin master && git stash pop

 와 같이 사용할 수 있습니다.

'Git' 카테고리의 다른 글

[Git] 브랜치 관리 전략  (0) 2023.02.03