git 'refusing to merge unrelated histories' 해결 (GitHub README 만든 뒤 pull)
GitHub에서 README를 넣어 저장소를 만든 뒤, 내 컴퓨터 프로젝트를 연결해 git pull 했더니 fatal: refusing to merge unrelated histories가 나왔다면, 두 저장소의 커밋 기록에 공통 조상이 하나도 없어서 git이 합치기를 거부한 것입니다. 해결은 옵션 하나입니다.
git pull --no-rebase origin main --allow-unrelated-histories
git push origin main
아래 내용은 2026년 11월 20일 리눅스에서 git 2.43.0으로 "README만 있는 원격 저장소 + 따로 만든 로컬 프로젝트" 상황을 직접 만들어 확인한 결과입니다.
1. 이런 순서로 하면 생깁니다
- GitHub에서 새 저장소를 만들며 "Add a README" 체크 → 원격에
Initial commit1개 - 내 컴퓨터에서
git init후 따로 커밋하고,git branch -M main으로 브랜치 이름을 main으로 맞춤 → 로컬에first1개 (설정이 없으면 git init 기본 브랜치가 master라서, 이걸 안 하면 push 때src refspec main does not match any가 먼저 납니다) git remote add origin …후 push → 거절
! [rejected] main -> main (fetch first)
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.
그래서 pull을 해야 하는데, 그냥 git pull은 fatal: Need to specify how to reconcile divergent branches.로 먼저 멈춥니다. merge 방식(--no-rebase)을 붙여 pull 하면 이 오류가 나옵니다.
git pull --no-rebase origin main
fatal: refusing to merge unrelated histories
2. 해결: --allow-unrelated-histories
git pull --no-rebase origin main --allow-unrelated-histories
직접 실행하니 원격의 README.md가 들어오고, 두 기록을 잇는 merge 커밋이 생겼습니다.
* 78e8a39 Merge branch 'main' of ../gh
|\
| * d421ca5 Initial commit
* 6334b84 first
이후 git push origin main은 정상으로 올라갔습니다.
3. 같은 이름 파일이 양쪽에 있으면 (충돌)
로컬에도 README.md가 있으면 위 명령이 이렇게 멈춥니다.
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
README.md를 열어 <<<<<<< ~ >>>>>>> 부분을 정리한 뒤 git add README.md → git commit → git push 순서로 마무리합니다.
4. rebase로 pull 하면?
같은 상황에서 git pull --rebase origin main을 해 보니 오류 없이 내 커밋이 원격 Initial commit 위로 옮겨졌습니다(merge 커밋 없이 한 줄 기록). 기록을 깔끔하게 두고 싶으면 이 방법도 됩니다.
ab68ef3 first
6e7604f Initial commit
5. 처음부터 안 생기게 하려면
- 이미 로컬 프로젝트가 있다면 GitHub 저장소를 만들 때 README·.gitignore·라이선스를 체크하지 않고 빈 저장소로 만든 뒤 push 합니다.
- 또는 GitHub 저장소를 먼저
git clone하고, 그 폴더에 프로젝트 파일을 옮겨 커밋합니다.
주의: 전혀 다른 프로젝트 저장소를 잘못 연결한 경우에도 이 옵션을 쓰면 그냥 합쳐집니다. git remote -v로 주소가 맞는지 먼저 확인하세요.
6. 한눈에 정리
| 상황 | 명령 |
|---|---|
| refusing to merge unrelated histories | git pull --no-rebase origin main --allow-unrelated-histories |
| 기록을 한 줄로 | git pull --rebase origin main |
| add/add 충돌 | 파일 정리 → git add → git commit |
| 예방 | 로컬 프로젝트가 있으면 GitHub 저장소를 빈 상태로 만들기 |
실행 환경: 리눅스, git 2.43.0, 로컬 bare 저장소로 GitHub 원격을 흉내 냄. GitHub 화면과 윈도우·맥에서는 직접 실행하지 않았습니다. 초안 작성과 검수에 AI를 사용했고, 명령과 출력은 직접 실행해 대조했습니다.
기준일: 2026-11-20
출처
- git-merge 문서 (--allow-unrelated-histories): https://git-scm.com/docs/git-merge
- git-pull 문서: https://git-scm.com/docs/git-pull