[Git] rejected non-fast-forward
- Git
- 2025. 10. 9.
반응형
반응형
문제상황 : 로컬 be branch에서 원격의 main branch로 push 하려고 했을 때 다음과 같은 에러가 발생했다.
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/username/repository.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you want to integrate the remote changes, use 'git pull'
hint: before pushing again.
문제 발생 이유 : 원격의 main branch가 로컬의 main branch보다 앞서 있는 상태이다.
따라서 local main이 outdated(구버전) 상태로 fast-forward merge가 불가능하다.
해결방안 1. --allow-unrelated-histories
완전히 별개의 두 Git History를 병합하고자 할 때 사용한다.
하지만 현재 상황은 같은 프로젝트의 같은 브랜치로 공통 조상이 존재한다.
따라서 Git History가 지저분해지기에 적절하지 않다.
해결방안 2. git push -f
원격의 최신 커밋들을 강제로 덮어쓴다.
혼자 작업하는 프로젝트라면 상관없겠지만 협업 중인 프로젝트이기에 적절하지 않다.
✨ 해결방안 3. main branch로 이동 후 병합한다. ✨
git checkout main #main 브랜치로 전환
git pull origin main #원격의 main 최신 내용 가져오기
git merge be # be 브랜치의 변경사항을 main에 병합한다.
충돌되는 코드가 있다면 해결 후 병합한다.
마지막으로 원격 브랜치에 푸시한다.
git push origin main
결론 : 커밋하기 전에 항상 원격 브랜치를 pull한뒤 작업하자.
반응형