fatal: refusing to merge unrelated histories: 얕은 클론인지 먼저 본다fatal: refusing to merge unrelated histories: Check for a shallow clone first
이 오류를 만난 독자가 기록이 잘려서 공통 조상이 안 보이는 것인지 정말 따로 시작한 기록인지 가려 맞는 처방을 고르게 한다.
같은 프로젝트의 얕은 클론에서도 같은 거부가 났고 git fetch --unshallow 뒤에는 옵션 없이 병합됐다. 전체 기록을 받아도 공통 조상이 없을 때만 --allow-unrelated-histories 를 검토하며, 같은 이름의 파일이 다르면 add/add 충돌을 직접 풀어야 한다.
git rev-parse --is-shallow-repository 를 실행해 true 면 git fetch --unshallow 부터 하고, git merge-base 로 공통 조상이 생겼는지 확인한다.
한 줄 요약:
refusing to merge unrelated histories는 git 이 두 갈래의 공통 조상을 찾지 못했다는 뜻이며, 얕은 클론이면git fetch --unshallow로 기록을 마저 받는 것이 먼저다.
바로 할 것: 병합을 다시 하기 전에 아래 세 줄로 상태부터 본다. main 자리에는 합치려던 브랜치 이름을, origin 자리에는 그 브랜치가 있는 원격 이름을 넣는다. 로컬 브랜치끼리라면 첫 줄은 건너뛰고 git merge-base HEAD 브랜치 로 본다.
git fetch origin main:refs/remotes/origin/main
git rev-parse --is-shallow-repository
git merge-base HEAD origin/main
첫 줄은 상대 브랜치를 --depth 없이 가져온다. 깊이 1 클론에는 그 브랜치가 아직 없어 Not a valid object name 이 날 수 있어서다. 둘째 줄이 true 면 git fetch --unshallow 를 실행하고 둘째·셋째 줄을 다시 돌린다.
셋째 줄에서 커밋 해시가 한 줄 나오면 옵션 없이 병합한다(git merge --no-edit origin/main). 아래 “따로 시작한 두 저장소” 쪽은 둘째 줄이 false 인데도 셋째 줄이 아무것도 안 찍고 끝날 때만 해당한다. 기록을 받은 뒤에도 true 면 아직 단정하지 않는다. git 문서는 원본 저장소가 얕으면 --unshallow 가 원본이 가진 만큼만 받는다고 하므로, 전체 기록을 가진 저장소를 원격으로 다시 확인해야 한다(이 경우는 시험하지 않았다).
같은 문구, 다른 두 상황
공통 조상은 두 브랜치가 갈라지기 전에 함께 가진 커밋이다. git 문서는 git merge 가 공통 조상이 없는 기록의 병합을 기본으로 거부한다고 적는다. 그런데 조상이 정말 없는 경우와, 있는데 내 저장소에 내려받지 않은 경우가 같은 문구로 끝난다.
fatal: refusing to merge unrelated histories
git merge-base 만으로는 둘을 가를 수 없다. 두 상황 모두 아무 출력 없이 종료 코드 1 이었다. 먼저 볼 값은 얕은 저장소 여부다. 얕은 클론(shallow clone)은 git clone --depth 1 처럼 최근 커밋만 받은 복사본이다.
| 시험한 상황 | --is-shallow-repository |
merge-base |
전체 기록을 받은 뒤 |
|---|---|---|---|
따로 git init 한 두 저장소 |
false |
출력 없음, 1 | 해당 없음 |
| 같은 프로젝트의 얕은 클론 | true |
출력 없음, 1 | 공통 조상이 나옴 |
| 얕은 클론에 전혀 다른 저장소를 원격으로 추가 | true |
출력 없음, 1 | 여전히 출력 없음 |
표의 셋째 줄 때문에 true 라고 해서 원인이 확정되지는 않는다. 기록을 마저 받고 나서 merge-base 를 다시 봐야 한다.
얕은 클론: 기록을 마저 받는다
시험에서는 main 을 --depth 1 로 클론하고, 갈라진 feature 브랜치도 깊이 1 로 가져온 뒤 병합했다.
$ git merge origin/feature
fatal: refusing to merge unrelated histories
$ git fetch --unshallow
$ git rev-parse --is-shallow-repository
false
$ git merge --no-edit origin/feature
Merge made by the 'ort' strategy.
g.txt | 1 +
(fetch 앞뒤의 다른 확인 명령과 출력 끝의 1 file changed·create mode 줄은 생략했다.) feature 에 자체 커밋이 두 개인 구성에서도 인자 없는 git fetch --unshallow 뒤에 병합됐다. 브랜치를 --depth 없이 가져오는 일은 --unshallow 앞뒤 어느 쪽에 해도 공통 조상이 나왔다. 이미 전체 기록인 저장소에서 --unshallow 를 다시 주면 fatal: --unshallow on a complete repository does not make sense 가 나온다.
여기서 --allow-unrelated-histories 를 먼저 붙이면 결과가 달라진다. 같은 얕은 클론에 옵션을 줬더니, feature 는 갈라진 뒤 f.txt 를 건드리지 않았고 main 만 한 줄을 더했는데도 f.txt 가 add/add 충돌(양쪽이 같은 파일을 새로 만든 것으로 취급)이 됐다. 공통 조상이 안 보이니 git 이 어느 쪽이 바꾼 것인지 비교할 기준이 없었다.
충돌 상태에서는 git merge --abort 로 빠져나왔고, 작업 트리와 로그가 병합 전 그대로였다.
충돌이 안 나는 구성도 있다. feature 가 새 파일만 더하고 겹치는 파일의 내용이 같은 시험에서는 옵션을 준 병합이 종료 0 으로 끝났다. 이 병합은 공통 조상을 비교 기준으로 쓰지 못한 채 계산된다. 방금 만든 커밋을 push 하기 전이라면 아래처럼 되돌리고 기록을 받은 뒤 다시 병합할 수 있다. --abort 와 reset --keep 은 커밋하지 않은 변경이 없는 상태에서만 시험했으므로, 병합을 하기 전에 먼저 커밋하거나 치워 두는 편이 안전하다.
git reset --keep ORIG_HEAD
git fetch --unshallow
git merge --no-edit origin/feature
ORIG_HEAD 는 직전 병합 전 위치라서 병합 직후, 다른 작업을 하기 전에만 쓴다. 시험에서는 작업 트리가 깨끗했고 로그가 병합 전 커밋으로 돌아갔다. git 문서는 병합을 시작할 때 커밋하지 않은 변경이 있었으면 git merge --abort 가 원래 상태를 되살리지 못하는 경우가 있다고 적는다.
따로 시작한 두 저장소: 옵션을 붙이기 전에
예를 들면 로컬에서 git init 해 커밋하고, 원격에는 README 나 LICENSE 를 담아 저장소를 만든 뒤 당기는 경우다. 먼저 git remote -v 로 주소가 맞는 프로젝트인지 확인한다. 주소를 잘못 넣은 것이라면 옵션이 아니라 git remote set-url 로 고친 뒤 다시 fetch 해서 merge-base 를 확인한다. 두 기록의 첫 커밋은 git rev-list --max-parents=0 HEAD 와 git rev-list --max-parents=0 origin/main 으로 볼 수 있고, 시험에서는 서로 다른 해시였다. 얕은 저장소에서는 잘린 지점이 첫 커밋처럼 나오므로 이 비교는 false 일 때만 뜻이 있다.
두 기록을 정말 합칠 생각이면 이렇게 한다.
git pull origin main --allow-unrelated-histories --no-rebase --no-edit
origin main 의 main 은 합치려는 브랜치 이름으로 바꾼다. --no-edit 는 병합 메시지 편집기를 열지 않고 기본 메시지로 커밋한다.
파일 이름이 겹치지 않는 시험(로컬 app.txt, 원격 LICENSE)은 바로 병합돼 두 파일이 모두 남았다. 양쪽에 내용이 다른 README.md 가 있으면 멈춘다.
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
그만두려면 git merge --abort 를 쓴다. 파일을 열어 <<<<<<<, =======, >>>>>>> 표시를 지우며 남길 내용을 고른 다음 git add README.md 와 git commit --no-edit 를 실행한다. 시험에서는 그 뒤 merge-base 가 원격의 커밋을 돌려줬다. git 문서는 이 옵션을 따로 시작한 두 프로젝트의 기록을 합치는 드문 경우를 위한 것으로 적고, 기본으로 켜는 설정은 없다고 한다.
같은 프로젝트였는데 원격의 기록이 새로 쓰여(강제 push 등) 조상이 사라진 경우라면 옵션을 붙이기 전에 원격 이력이 왜 바뀌었는지부터 확인한다. 이 경우는 시험하지 않았다.
거부 문구 대신 다른 오류가 먼저 나올 때
pull.rebase 같은 설정이 하나도 없는 환경에서 갈라진 상태로 git pull origin main 을 하면, 위 문구가 아니라 아래에서 멈췄다.
fatal: Need to specify how to reconcile divergent branches.
이때는 병합할지 rebase 할지를 먼저 정해야 한다. 위 명령의 --no-rebase 는 병합을 고르는 뜻이다. git pull --rebase 를 택하면 거부 문구 없이 로컬 커밋을 원격 위에 다시 얹으려 했고, 겹친 README.md 에서 add/add 충돌로 멈췄다. 되돌리려면 git rebase --abort 다.
시험 범위와 한계
2026-12-03, macOS 27.2.1 arm64 의 git 2.54.0 (Apple Git-157)에서 로컬 bare 저장소를 원격으로 삼아 시험했고, 스크립트마다 두 번 실행해 결과가 같았다. 첫 비교 시험(따로 만든 저장소와 얕은 클론)은 pull.rebase=false 를 두었고 나머지는 두지 않았으며, 문구는 LANG=C 기준이다. GitHub 같은 호스팅 서비스, 다른 git 버전, 윈도·리눅스, GUI 클라이언트, CI 러너, 서브모듈은 시험하지 않았다. 원격 자체가 얕은 저장소면 git 문서상 원격이 가진 만큼만 받는다.
세 줄 요약
- 이 거부는 공통 조상이 정말 없을 때와, 얕은 클론이라 조상이 보이지 않을 때 모두 난다.
--is-shallow-repository가true면git fetch --unshallow뒤에git merge-base를 다시 확인한다.- 전체 기록에서도 조상이 없고 합치려는 저장소가 맞을 때만
--allow-unrelated-histories를 쓰고 충돌은 직접 푼다.
Help a reader who hit this error tell whether truncated history is hiding the common ancestor or the histories really started separately, and pick the matching fix.
A shallow clone of the same project produced the same refusal, and after git fetch --unshallow it merged without any option. Consider --allow-unrelated-histories only when there is still no common ancestor with full history; differing files with the same name then become add/add conflicts you resolve yourself.
Run git rev-parse --is-shallow-repository; if it prints true, run git fetch --unshallow first and check with git merge-base that a common ancestor appeared.
Contents
One-line summary:
refusing to merge unrelated historiesmeans git could not find a common ancestor of the two lines of history, and if this is a shallow clone, fetching the rest of the history withgit fetch --unshallowcomes first.
Do this now: Before trying the merge again, check the state with the three lines below. Put the name of the branch you meant to merge where main is, and the name of the remote that has it where origin is. Between local branches, skip the first line and check with git merge-base HEAD <branch>.
git fetch origin main:refs/remotes/origin/main
git rev-parse --is-shallow-repository
git merge-base HEAD origin/main
The first line fetches the other branch without --depth, because a depth-1 clone may not have that branch yet and would answer Not a valid object name. If the second line prints true, run git fetch --unshallow and run the second and third lines again.
If the third line prints one commit hash, merge without any extra option (git merge --no-edit origin/main). The section on two separately started repositories below applies only when the second line is false and the third line still ends without printing anything. If it is still true after fetching, do not conclude anything yet. The git documentation says that when the source repository is shallow, --unshallow only gets as much as the source has, so you need to check again against a repository that has the full history (I did not test this case).
Same message, two different situations
A common ancestor is a commit both branches had before they diverged. The git documentation says git merge by default refuses to merge histories that do not share one. But the case where there truly is no ancestor and the case where there is one that was never downloaded into your repository end with the same message.
fatal: refusing to merge unrelated histories
git merge-base alone cannot separate the two. Both situations ended with no output and exit code 1. The value to look at first is whether the repository is shallow. A shallow clone is a copy that received only recent commits, such as one made with git clone --depth 1.
| Tested situation | --is-shallow-repository |
merge-base |
After fetching full history |
|---|---|---|---|
Two repositories each started with git init |
false |
no output, 1 | not applicable |
| Shallow clone of the same project | true |
no output, 1 | a common ancestor appears |
| A completely different repository added as a remote to a shallow clone | true |
no output, 1 | still no output |
Because of the third row, true does not settle the cause. You have to look at merge-base again after fetching the rest.
Shallow clone: fetch the rest of the history
In the test, main was cloned with --depth 1, the diverged feature branch was also fetched at depth 1, and then merged.
$ git merge origin/feature
fatal: refusing to merge unrelated histories
$ git fetch --unshallow
$ git rev-parse --is-shallow-repository
false
$ git merge --no-edit origin/feature
Merge made by the 'ort' strategy.
g.txt | 1 +
(Other check commands around fetch and the trailing 1 file changed and create mode lines are omitted.) In a setup where feature had two commits of its own, the merge also worked after a plain git fetch --unshallow. Fetching the branch without --depth, whether before or after --unshallow, also produced the common ancestor. Passing --unshallow again to a repository that already has full history gives fatal: --unshallow on a complete repository does not make sense.
Adding --allow-unrelated-histories first changes the result. With the option on the same shallow clone, f.txt became an add/add conflict (treated as if both sides had newly created the same file), even though feature did not touch f.txt after diverging and only main added one line. With no common ancestor visible, git had nothing to compare against to tell which side had changed it.
From the conflicted state I got out with git merge --abort, and the working tree and log were as before the merge.
There are also setups without a conflict. In a test where feature only added a new file and the overlapping files had identical content, the merge with the option finished with exit 0. That merge is computed without using a common ancestor as a basis for comparison. If you have not pushed the commit you just made, you can undo it as below, fetch the history, and merge again. --abort and reset --keep were tested only with no uncommitted changes, so it is safer to commit or set work aside before you merge.
git reset --keep ORIG_HEAD
git fetch --unshallow
git merge --no-edit origin/feature
ORIG_HEAD is the position before the last merge, so use it only right after the merge, before doing anything else. In the test the working tree was clean and the log went back to the pre-merge commit. The git documentation says that if there were uncommitted changes when the merge started, git merge --abort is in some cases unable to restore the original state.
Two separately started repositories: before adding the option
For example, you run git init and commit locally, create a remote repository containing a README or LICENSE, and then pull. First check with git remote -v that the address points to the right project. If you entered the wrong address, fix it with git remote set-url rather than the option, then fetch again and check merge-base. You can see the first commit of each history with git rev-list --max-parents=0 HEAD and git rev-list --max-parents=0 origin/main; in the test the hashes differed. In a shallow repository the cut-off point shows up like a first commit, so this comparison only means something when the value is false.
If you really mean to combine the two histories, do this.
git pull origin main --allow-unrelated-histories --no-rebase --no-edit
Replace main in origin main with the branch you are merging. --no-edit commits with the default message without opening the merge message editor.
The test with no overlapping file names (local app.txt, remote LICENSE) merged right away and kept both files. If both sides have a README.md with different content, it stops.
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
To give up, use git merge --abort. Otherwise open the file, choose what to keep while removing the <<<<<<<, ======= and >>>>>>> markers, then run git add README.md and git commit --no-edit. In the test, merge-base returned the remote commit afterwards. The git documentation describes this option as being for the rare case of combining the histories of two projects that started independently, and says there is no setting that turns it on by default.
If it was the same project but the remote history was rewritten (a force push, for example) so that the ancestor disappeared, find out why the remote history changed before adding the option. I did not test this case.
When a different error comes before the refusal
In an environment with no setting such as pull.rebase, running git pull origin main with diverged branches stopped at the message below rather than the one above.
fatal: Need to specify how to reconcile divergent branches.
In that case you first have to decide whether to merge or rebase. The --no-rebase in the command above chooses merge. Choosing git pull --rebase tried to replay the local commit on top of the remote without any refusal message, and stopped with an add/add conflict on the overlapping README.md. To go back, use git rebase --abort.
Test scope and limits
Tested on 2026-12-03 with git 2.54.0 (Apple Git-157) on macOS 27.2.1 arm64, using local bare repositories as remotes; each script was run twice with the same result. The first comparison test (separately created repositories and a shallow clone) had pull.rebase=false set and the others did not, and messages are from LANG=C. Hosting services such as GitHub, other git versions, Windows and Linux, GUI clients, CI runners and submodules were not tested. If the remote itself is shallow, the git documentation says you only get as much as the remote has.
Three-line summary
- This refusal appears both when there truly is no common ancestor and when a shallow clone hides it.
- If
--is-shallow-repositoryistrue, checkgit merge-baseagain aftergit fetch --unshallow. - Use
--allow-unrelated-historiesonly when there is no ancestor even with full history and the repository you are merging is the right one, and resolve conflicts yourself.