발생
A 브랜치에서 커밋한 뒤 B 브랜치로 변경하려고 하는데 다음과 같은 erorr가 발생함.
wk@MacBookm1-Pro seb40_main_025 % git switch -t origin/Feat/#37
error: Your local changes to the following files would be overwritten by checkout:
server/oneyearfourcut/src/test/java/com/codestates/mainproject/oneyearfourcut/artwork/repository/ArtworkRepositoryTest.java
Please commit your changes or stash them before you switch branches.
Aborting
이러한 error는 브랜치 변경 뿐만 아니라 remote pull할 때도 자주 접했던 에러이다.
하도 자주 보니... 따로 정리를 해두려고 한다.
// pull 에러
error: Your local changes to the following files would be overwritten by merge:
...
Please commit your changes or stash them before you merge.
// 브랜치 변경 에러
error: Your local changes to the following files would be overwritten by checkout:
...
Please commit your changes or stash them before you switch branches.
원인
에러가 발생하는 이유는 현재 작업한 소스와 remote repository의 소스의 차이가 생겨 충돌이 일어난 것이다.
해결
메세지를 잘 살펴보면 commit을 하거나 stash를 하라는 것을 알 수 있다.
방법 1. git commit or git add
메세지에서도 나와 있듯이 commit을 진행하면 정상적으로 명령어를 실행시킬 수 있다.
덧붙이면 commit까지 할 필요없이 git add로 수정된 파일을 staging 영역에 올려도 해결할 수 있다.
// 예시
% git add<file> .
// 사실상 여기까지만 진행해도 상관없다...
% git commit -m "커밋 메세지"
방법 2. git stash
git stash 명령어는 간단하게 수정된 파일을 임시 저장한 후 다른 곳으로 보내는 역할 한다고 생각하면 된다.
git stash를 사용하면 스택으로 수정된 파일을 보내기 때문에 pop으로 꺼내올 때까지 로컬에서 사라집니다.
작업한 내용이 안 보이더라도 당황하지마세요!
// 버전관리 대상의 파일을 staging 영역의 스택에 잠깐 넣어둔다.
% git stash
// remote repository로 부터 pull을 한다.
% git pull origin 브랜치명
// 넣어둔 파일을 꺼내온 뒤 변경사항을 적용시키고, stack에서 제거한다.
% git stash pop
'Git > Trouble Shooting' 카테고리의 다른 글
[Trouble Shooting] git pull - merge 실패 "fatal: Need to specify how to reconcile divergent branches." (0) | 2022.11.19 |
---|