3

I did create a remote repo ('myRepo') on Git. I checked the checkbox 'add a READ.me', which also make a commit on this 'myRepo' repo.

Then I create a new folder, where I added somes files and did :

git init
git add .
git commit -m "init"
git remote add origin https://github.com/jozinho947/myRepo.git

# get the READ.me fetched on the 'origin/master' branch locally
git fetch

# get the READ.me merged in my local 'master' where i'm pointing
git merge origin/master

And then I have this error :

fatal: refusing to merge unrelated histories

I don't understand why I can't do this... Can somebody help me ?

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
jozinho22
  • 459
  • 2
  • 7
  • 24
  • Try cloning it first without having an existing repo. When you create a new repo it has a different history so git is refusing to combine the two without you saying it’s ok. – mrmcgreg May 12 '20 at 14:10
  • 1
    You can saying it’s ok by allowing unrelated histories. – mrmcgreg May 12 '20 at 14:11
  • Does this answer your question? [Git refusing to merge unrelated histories on rebase](https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase) – Michael Freidgeim Jul 06 '22 at 22:52

2 Answers2

10

The remote copy of your repo has a root commit, with no parent -- the 1st commit with a README.md file.

With the actions you performed locally, your local repository also has a root commit with no parent.

git merge refuses to work because it cannot find a common ancestor to these two commits.


You probably want to replay your commits one after the other, use git rebase instead of git merge :

git rebase origin/master

This should apply your local root commit on top of the "README" commit.


If you want to replay them in the opposite order (remote commit on top of your local commit) : use git cherry-pick origin/master. You will then need to force push your master branch.

If you have good reason to keep two root commits in your repo : you can use the --allow-unrelated-histories option of git merge.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Exactly, thank you very much. As well, like 'mrmcgreg ' said above, i can just delete the existing local .git repo and git clone the remote. – jozinho22 May 12 '20 at 14:51
  • 2
    Alternatively you might have said `git pull --rebase origin master`, but that is in fact the same as the given two step solution, `git fetch` followed by rebase. – matt May 12 '20 at 14:57
  • same issue none worked for me this did... superb. thanks a lot :) – Atif Afridi Apr 14 '22 at 20:29
0

Try pulling with this git command git pull origin main --allow-unrelated-histories.

This should fetch the main branch from your remote repository and merge it with your local repo, afterwards you will push your code to the remote repository using the git push -u origin main.

You got that error message because a commit was made to the remote repository, like when you allowed Github to initialize a README file on your behalf. But since your local repository is missing that README file, hence the message: fatal refusing to merge unrelated branches

Bravo Stack
  • 111
  • 1
  • 6