error: failed to push some refs to 'https://github.com/HeWillGoTillTheEnd/test2Repo.git'
To https://github.com/HeWillGoTillTheEnd/test2Repo.git
hint: Updates were rejected because the remote contains work that you do
! refs/heads/main:refs/heads/main [rejected] (fetch first)
Done
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를 하던 도중 Push가 Reject 되며 다음과 같은 에러를 만났다
다음과 같이 설정되어 있어 이유를 알기가 어려웠는데 문제는 각각 따로 리포지토리를 생성해 리포지토리의 히스토리가 각각 달라진게 문제였다
로컬 리포지토리는 IntelliJ로 생성하여 git init를 통해 로컬 리포지토리를 생성했고
원격 리포지토리는 README.md를 추가해서 만들었다
이후 IntelliJ의 Define Remote를 통해 원격리포지토리의 주소값을 로컬 리포지토리에 등록해주고 Push를 했는데
다음과 같은 경우 원격 리포지토리는 README.md를 만들며 git의 히스토리가 생성이 되고 로컬리포지토리는 git init를 하게 되면서 각자 다른 히스토리가 생성이 되어 Push를 할 경우 충돌이 발생하며 Reject 된것이다
문제 해결은 어떻게?
문제 해결을 위해 여러 방법을 시도하고 분석해보며 블로그를 찾던 도중 다음과 같은 블로그를 찾을 수 있었다.
https://gdtbgl93.tistory.com/63
Git push가 안되는 경우 (fatal: refusing to merge unrelated histories)
로컬 저장소에 있는 프로젝트를 깃허브 사이트를 통해 만든 저장소로 push 하는 경우에 이런 메세지가 뜨는 경우가 있다. 12345678C:\Users\gitProject>git push origin masterTo https://github.com/userId/userProject.git !
gdtbgl93.tistory.com
git pull origin 브런치명 --allow-unrelated-histories
위의 코드를 통해 이미 존재하는 두 프로젝트의 히스토리를 병합하는 상황에 사용한다는 정보를 입수해서 직접 실험해보았다.
두개의 다른 히스토리가 합쳐졌다! 보면 First Commit(로컬 리포지토리)와 Initial commit(원격 리포지토리)가 병합되었음을 알 수 있다.
마지막으로 답을 찾은 블로그에 마지막 내용으로 글을 마친다.
'공부 > TIL' 카테고리의 다른 글
Type parameter 'E' hides type parameter 'E' (0) | 2023.08.31 |
---|---|
정처기 - 2 (0) | 2023.07.06 |
정처기 정리 (0) | 2023.07.05 |
바이트코드, 바이너리코드 (0) | 2023.06.30 |
오늘 한것들 (1) | 2023.05.12 |