Git

[Git] ! [rejected] main -> main (fetch first)error: failed to push some refs... 원인 및 해결 방법

jimmy_AI 2024. 1. 28. 17:17
반응형

git push 과정에서 conflict로 인하여 자주 발생할 수 있는

오류 메시지에 대하여 원인과 대처 방법을 간략히 정리해보도록 하겠습니다.

 

 

에러 개요

git push를 하는 과정에서 다음과 같은 오류 메시지가 발생하면서,

push가 거절당했습니다.(브랜치 이름이 main인 경우를 기준으로 가정합니다.)

To git.projectbro.com:project_name/repo_name
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'git.projectbro.com:project_name/repo_name'
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.

 

 

원인

다른 곳에서 push가 이루어져

현재 로컬 내의 레포 상태가 github(gitlab) 사이트 내의 내용과 동기화가 안되어 있고,

이로 인하여 변경 사항에 충돌이 발생하여 해결이 필요한 상황입니다.

 

반응형

 

해결 방안

1. 먼저, 작업하여 push를 시도했던 내용을 임시 저장하고, 마지막 커밋 상태로 되돌립니다.

git stash

 

2. github(gitlab)의 원격 레포와 현재 로컬 내의 레포를 동기화 합니다.

(main 브랜치 기준 명령어이며, main 자리에 원하는 브랜치를 적어주시면 됩니다.)

git pull origin main

 

3. 임시 저장했던 내용을 다시 반영해줍니다.

git stash pop

 

4. 다시 push를 시도해주면 정상적으로 작동이 가능합니다.

git push origin main