최근 git의 default branch를 master에서 main으로 변경했다.
먼저 프로젝트를 진행하다가 깃허브에 readme 파일을 포함한 리포지토리를 만들고, 이 리포를 remote으로 연결하고 내 코드를 push하려고 했다.
git remote add origin https://github.com/[사용자이름]/[repository이름].git
으로 remote를 연결하고,
git remote -v
를 통해 제대로 연결됐는지 확인했다.

이런식으로 나오면 잘 된 것이다.
이후 push를 하려고 할 때
git push -u origin master(or main)
이 명령어를 입력한다. 나의 경우 default branch가 main이므로 main에 push해야하는데 master에 해버려서 원치 않는 master branch가 새로 생겼다. pull request를 하려고 해도 collaborator가 없어서 그런지 버튼이 없었다.
그래서 다시 main에 push하고 master branch를 지우고자 했다.
git push origin main
을 하니까
To https://github.com/hyunseo-k/nice_app.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/hyunseo-k/nice_app.git'
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에 나와있는 대로
git pull
을 했다.
fatal: refusing to merge unrelated histories
라는 오류가 떴다.
공통 부분이 없어서 발생하는 것이라고 한다.
이때는
git pull origin main --allow-unrelated-histories
를 입력하면 된다.
이후 다시
git push origin main
을 하면 해결된다!
참고:
https://velog.io/@seo0ojin/GitHub-main-branch%EC%97%90-push%ED%95%98%EA%B8%B0
https://velog.io/@jeongm/git-remote-repository-connect
https://somjang.tistory.com/entry/Git-fatal-refusing-to-merge-unrelated-histories-%ED%95%B4%EA%B2%B0-%EB%B0%A9%EB%B2%95