dukongmon

Github error : ! [rejected] main -> main (fetch first), (non-fast-forward) 본문

etc./Github

Github error : ! [rejected] main -> main (fetch first), (non-fast-forward)

duiiminish 2026. 4. 24. 18:27

애증의 깃허브...ㅎ

프로젝트가 하나 끝나서 가벼운 마음으로 업로드 하려고 했는데 ReadMe 파일 하나 때문에 개고생함

이번 포스팅의 결론 : 깃헙 사이트에서 업데이트 사항이 있으면 항상 pull을 먼저 하고 push 하자!ㅠㅎ휴휴ㅠ

최종 결과는 맨 아래 ↓↓↓↓↓


사건의 시작 : 깃헙에서 ReadMe 파일을 수정하고 'git pull'을 하지 않은 상태에서

한 개의 파일만 commit 하려함

 

$ git add <파일 주소>
$ git commit -m "커밋 내용"
$ git push origin branch
 

 

그랬더니 이런 에러 메시지가 뜸

' ! [rejected] main -> main (fetch first) '

To https://github.com/park-yeon-ju/EmotionDetector
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/park-yeon-ju/EmotionDetector'
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 명령어인 'git push origin branch'에다가

branch 앞에 +하나만 붙여주면 된다고해서 'git push origin +branch'를 했더니

push가 되긴 되는데 폴더 안에 있는 파일들이 몽땅 올라갔다.

아마 강제로 전체 push가 된 것 같다.

$ git push origin +branch
 

 

원하던 push를 성공하긴 했지만... 생각한 방식으로 push가 된게 아니라서

다른 파일로 한 개의 파일만 commit하는 것을 재도전 해봤는데 역시 같은 오류가 떴다.

' ! [rejected] main -> main (fetch first) '

 

또 검색하다 보니 해당 에러는 pull을 하지 않은 상태에서 push를 하려했기 때문에 발생한 에러이므로

pull을 해주면 해결된다고 함.

$ git pull
$ git push origin branch
 

git pull을 해봤더니 뭔가 pull되긴 함.


 

됐다ㅎ 하고 push 해보니까 이번엔 다음과 같은 에러가 뜸....

' ! [rejected] main -> main (non-fast-forward) '

! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/park-yeon-ju/EmotionDetector'
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.
 

 

깃헙 공식 사이트에서 시키는 대로 fetch -> merge -> pull 해봤는데 안먹힘.

 

 

비빠른 선행 오류 처리 - GitHub 문서

Git에서 커밋을 잃지 않고 원격 리포지토리를 변경할 수 없는 경우가 있습니다. 이 경우 푸시가 거부됩니다.

docs.github.com

 

fetch 했을 때는 아무 메시지도 뜨지 않았고, merge 했을 때는 merge 할게 없다고 뜸.


 

그런데 아까부터 pull 할 때마다

" fatal: Need to specify how to reconcile divergent branches. "

이런 메시지가 뜨길래 저게 문제인가 싶어 검색 후 관련 명령어를 날려봤지만 저게 문제가 아니었나봄.

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

 

 

이때 맨 밑줄을 볼게 아니라 힌트를 봤으면 빨리 끝났을텐데..ㅎ

여튼 좀 더 검색을 해보다 hint에 있는 내용을 하라는 게시물 발견.

 

위에 있는 hint 속 세가지 git 코드들 중

merge, rebase, fast-forward 중 해당되는 상황에 맞는거 선택해서 그 명령어를 날려주라함.

 

참고 링크 :

 

git pull 할 때 fatal: Need to specify how to reconcile divergent branches. 오류

현재 내 깃 버전은 다음과 같다. 어떤 버전에서부터인진 모르겠지만 이전 버전에서는 git pull을 할 때 자동으로 merge 전략을 사용한 것 같지만 최신 버전에서는 어떤 전략을 사용할지 따로 지정되

velog.io

 

 

그래서 merge에 해당하는 명령어로 날려주고 pull 했더니

뭔가 왕창 삭제되고 있다는 코드가 주르르르 나옴

$ git config pull.rebase false # merge
$ git pull origin branch 

 

 

뭐지...? 하고 멍때리다가 실행이 끝나고 깃이랑 원격 연결되어있는 폴더 가보니까

ㅎ 깃에 올려놓은 파일들이 다 pull 되고, 컴퓨터의 내 폴더 안에 있던 파일들은 흔적조차 없이 사라짐....ㅎ

휴지통에 있나 가봤는데 휴지통에도 없음. 걍 사라짐.

 

다행히 삭제된 파일들은 크게 중요한 파일들이 아니라서 상관이 없긴 했지만

중요한 파일이었으면...하는 생각에 등골이 오싹해짐

이후 push 명령어 다시 날려주니 아주 깔끔하게 push 성공..ㅎ


 

이번 개고생으로 배운 점은

1. 깃 pull push용 폴더와 local에만 가지고 있을 파일이 있는 폴더는 구분해두자...

2. 깃 사이트 내에서 변경사항이 있을 때는 꼭 Pull을 먼저 하고 push를 하자..!

3. 협업할 때는 꼭꼭꼭꼭 branch를 나누어 작업하자 (오늘같이 파일 싹 증발했어봐...)


[ 최종 결론 ]

Error : ! [rejected] main -> main (fetch first)

원격 연결된 폴더 내용이 몽땅 upload 되도 괜찮으면 강제 push 방법인 'git push origin +branch' 사용

$ git push origin +branch 
# 기존 push 명령어인데 branch 앞에 + 하나 추가해주는 방식
 

 

Error : ! [rejected] main -> main (non-fast-forward)

아래 3가지 코드 중 본인 상황에 맞는 코드 쓰면 됨.

- git config pull.rebase false # merge

- git config pull.rebase true # rebase

- git config pull.ff only # fast-forward only

주의할 점은 merge 코드 + pull 코드 날려줄 경우

깃헙에 있는 내용이 원격 폴더에 그대로 덮어짐으로 중요한 파일 있으면 미리 빼놔야함.

$ git config pull.rebase false
$ git pull origin branch
$ git push origin branch