문제
원격 저장소에 리포지터리 작성 후 로컬에 있던 프로젝트를 push 하려는데 아래와 같은 오류 메시지 등장
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to '깃허브링크'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
원인
로컬 브랜치와 원격 브랜치의 히스토리가 달라서 발생
GitHub에서 리포지토리를 생성할 때 README나 .gitignore를 추가했다면 이런 상황이 발생할 수 있음
로컬에서 git pull을 먼저 진행 후 push 하면 됨
이때 git pull을 진행하려고 하면 아래 오류 메시지가 뜨면서 진행이 안 될 수 있음
fatal: refusing to merge unrelated histories
마찬가지로 로컬 브랜치와 원격 브랜치의 히스토리가 달라서 발생함
공통된 커밋 포인트가 없기 때문
해결
pull 명령어에 `--allow-unrelated-histories` 옵션 추가해서 강제 pull 받아오기
git pull origin 브랜치명 --allow-unrelated-histories
나 같은 경우 로컬 저장소에서 작성한 파일 내용이 날아갈까봐 원격 저장소의 README, gitignore 파일 삭제 후 git pull을 진행 함
근데 이럴 필요까진 없었던 듯
이때 아래와 같은 메시지가 뜰 수 있음
Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch
왜 해당 merge가 필요한지 내용을 남기라는 건데, vi 편집기를 사용하여 입력해야 하기 때문에 일반적인 입력 방법으로는 입력 불가
- `i` 입력하여 글자 입력 모드로 전환
- merge 이유에 대한 내용 작성
- `esc`키 눌러 입력 모드 벗어나기
- `:wq` 입력하여 저장 및 종료
- `enter`
참고
git push, pull (fatal: refusing to merge unrelated histories) 에러
원격 저장소를 remote로 설정하고 바로 push를 하면 몇가지 오류가 발생할 수도 있다.예를 들어 아래와 같은 오류 메시지이다.12 ! [rejected] master -> master (non-fast-forward)error: failed to push some refs to 'https:
jobc.tistory.com
Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic b
I am using Git. I did a pull from a remote repo and got an error message: Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a
stackoverflow.com