본문 바로가기
[ Laboratory ]/Lab etc

[Github] non-fast-forward error :: Updates were rejected because the remote contains work that you do not have locally

by dev charlotte 2024. 11. 20.

 

오류문

To 레포지토리 링크

! [rejected] main -> main (fetch first) error: failed to push some refs to '레포지토리 링크'

hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

오류 원인

로컬 브랜치와 원격 브랜치가 동기화되지 않아서 발생한 문제

non-fast-forward error

Updates were rejected because the remote contains work that you do not have locally

 

로컬에 없는 커밋이 원격 저장소에 있을 때 fast forward 병합이 불가능하기 때문에 푸시가 거부된다

 

두 가지 해결 방법이 있다

 

1. 충돌 직접 해결해주기

1-1. remote 레파지토리의 변경 사항을 먼저 로컬로 모두 가져온다

git pull origin main 

(main 브랜치 이름)

 

1-2. git pull에서 conflict 충돌이 발생했다면

충돌 파일을 열고 <<<<<<<, =======, >>>>>>> 표시된 부분을 수정

 

1-3. conflict 파일을 다시 스테이징

git add (파일명)

 

1-4. 다시 커밋

git commit -m "(commit message)"

 

1-5. 로컬 변경을 원격과 병합했으니 다시 푸시

git push origin (브랜치 이름)

 

 

2. 강제로 덮어쓰기

나는 원격 내용을 반영할 필요 없이 로컬 변경 사항으로 원격 브랜치를 덮어쓰고 싶었다

그래서 강제 푸시를 사용했으나 타인과 함께 사용한다면 주의해야한다

 

git push origin main --force