Git refuses to merge two branches that share no commits. Learn why this happens after git init plus a new remote repository, and how to merge safely with --allow-unrelated-histories.
The short answer
#Git stops because the two branches have no commit in common, so it has no starting point to merge from. If you are sure they belong together, which is usually a new local project and a freshly created remote that already has a README, tell Git to allow it:
_10git pull origin main --allow-unrelated-histories
Resolve any conflicts, commit if Git asks you to, then push.
Why it happens
#A normal merge looks for the most recent commit that both branches share, called the merge base, and combines the changes each side has made since then. Two histories that started from different root commits have no merge base at all.
Since Git 2.9, git merge and git pull refuse this by default, because merging two unrelated histories is usually a mistake, such as pulling from the wrong repository.
The common ways to end up here are:
- You ran
git initand committed locally, then created a repository on your Git host with a README, licence or.gitignore, and tried to pull it. Both sides now have their own first commit. - You added the wrong remote URL, so you are pulling a different project.
- The remote was recreated, or its history was rewritten from scratch, so its root commit is not the one you have.
- You are deliberately combining two separate projects into one repository.
How to fix it
#Check you have the right remote first
Before forcing anything, make sure the remote is the project you think it is.
_10git remote -v_10git fetch origin_10git log --oneline origin/main | tail -n 5_10git log --oneline main | tail -n 5
If origin points at the wrong repository, change the URL instead of merging:
_10git remote set-url origin [email protected]:your-name/your-repo.git
Merge with --allow-unrelated-histories
This is the usual fix when the remote only holds a starter commit and you want to keep both sides.
_10git fetch origin_10git merge origin/main --allow-unrelated-histories_10_10# Only if there were conflicts:_10git add README.md .gitignore_10git commit_10_10git push -u origin main
The result is a merge commit with two parents, one from each history.
Rebase your local commits on top of the remote
If you prefer a straight line of history, replay your local commits on top of the remote's starter commit instead.
_10git fetch origin_10git rebase origin/main_10git push -u origin main
This works well when you only have a handful of local commits. If a file such as README.md exists on both sides you will need to resolve it, then run git rebase --continue.
Replace the remote (only for a brand new project)
If the remote contains nothing but a generated README you do not want, and nobody else has cloned it, you can overwrite it with your local history.
_10git push --force-with-lease origin main
This deletes the remote's commits, so only do it on a repository you have just created.
Common pitfalls
#- Adding the flag out of habit. If the histories are unrelated because you pointed at the wrong repository, you will mix two different codebases together.
- Expecting a permanent setting.
--allow-unrelated-historiesis a one-off option forgit mergeandgit pull; there is no config key for it, and you only need it for the first merge. - Being surprised by conflicts. Files with the same path on both sides, such as
README.mdor.gitignore, show up as add/add conflicts that you resolve by hand. - Force pushing over a remote that other people have already cloned. Their copies will no longer match.
- Avoiding the problem next time is easy: when you already have a local project, create the remote repository empty, without a README or licence, then push to it.
Related reading
#Was this resource helpful?