프로젝트를 생성하고 변경사항 커밋 후 푸시를 했는데 아래와 같은 에러가 발생했다.
User@DESKTOP-BCO5GBM MINGW64 ~/IdeaProjects/(프로젝트 명) (main)
$ git push origin main
To https://github.com/...
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/...
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.
레퍼지토리를 생성하는 과정에서 README 파일을 만들고 pull을 하지 않은 상태에서 바로 push를 하면 에러가 발생한다고 한다.
$ git pull origin main
pull을 해서 레퍼지토리의 최신 정보를 받아오자.
위와 같이 pull을 했을 때 에러가 해결이 될 수도 있지만 아래와 같은 에러가 또 발생할 수도 있다.
User@DESKTOP-BCO5GBM MINGW64 ~/IdeaProjects/(프로젝트명) (main)
$ git push origin main
To https://github.com/...
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/...
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
위의 오류는 데이터 유실 등의 문제가 발생할 수 있어서 git에서 처리되지 않도록 에러를 발생시키고 사용자에게 경고 메시지를 전달해주는 것이라고 한다.
근본적인 원인을 찾아서 해결할 수 있겠지만 프로젝트를 지금 막 생성한 상황에서는 강제로 push를 하는 방법을 사용하자.
$ git push origin +main
브랜치 명 앞에 '+'를 넣어주면 push가 잘 된 것을 확인할 수 있다!