git push시 발생하는「Updates were rejected because the remote contains work that you do」문제 해결

git push시 발생하는「Updates were rejected because the remote contains work that you do」문제를 해결하는 방법에 대해서 정리해 봤습니다.
2024.04.02

안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 git push시 발생하는「Updates were rejected because the remote contains work that you do」문제를 해결하는 방법에 대해서 정리해 봤습니다.

문제 발생

git push 명령어를 입력했더니 다음과 같은 에러 메시지가 출력됐습니다.

error: failed to push some refs to ‘https://github.com/xxxxxxxxx/xxxxxxxxx.git’ 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.

이 에러는 원격 리포지토리가 변경되었기 때문에 발생하는 에러입니다.

즉, 다른 누군가가 먼저 git push를 해버려 원격 리포지토리와 로컬 리포지토리의 내용물이 일치하지 않기 때문에 발생하는 에러입니다.

문제 해결

문제 해결 방법으로는 먼저 git pull을 이용해 원격 리포지토리와 로컬 리포지토리의 내용물을 맞추는 것입니다.

git pull origin master
git push

즉, 로컬 리포지토리의 브랜치를 원격 리포지토리의 master 브랜치와 병합한 후, 변경 내용을 다시 push 합니다.

다른 방법으로는 -f 옵션을 사용하는 방법이 있습니다.

git push -f origin master

-f 옵션은 강제로 변경 사항을 리포지토리에 push하는 명령어입니다. 기존 변경 사항을 무시하고 강제로 push하는 옵션이기 때문에 주의할 필요가 있습니다.

-f 옵션을 사용하면 (forced update) 라는 메시지가 뜨며 강제로 push가 이루어진 것을 확인할 수 있습니다.

Enumerating objects: 70, done.

Counting objects: 100% (70/70), done.

Delta compression using up to 8 threads

Compressing objects: 100% (67/67), done.

Writing objects: 100% (70/70), 4.58 MiB | 4.07 MiB/s, done.

Total 70 (delta 5), reused 0 (delta 0), pack-reused 0

remote: Resolving deltas: 100% (5/5), done.

To https://github.com/xxxxxxxxx/xxxxxxxxx.git

+ df52b31...89f5df4 master -> master (forced update)

이번에 이러한 문제가 발생했던 이유는 리포지토리에서 불필요한 파일을 제거하려고 했기 때문입니다.

로컬 리포지토리에서 파일을 삭제한 다음, git push를 한 것이 아닌, GitHub 원격 리포지토리에서 수동으로 파일을 삭제했기 때문에 로컬 리포지토리와 원격 리포지토리의 내용물이 맞지 않아 충돌이 발생한 것입니다.

다수의 사용자가 관여하는 프로젝트의 경우, GitHub를 사용할 때 주의해야 합니다. 브랜치 관리, 충코드 리뷰, 커뮤니케이션 등 이러한 주의 사항을 준수하여 프로젝트를 수행할 필요가 있습니다.

본 블로그 게시글을 읽고 궁금한 사항이 있으신 분들은 kis2702@naver.com로 보내주시면 감사하겠습니다.