15

I've added a remote repository to the folder where I am working with:

git remote add origin https://github.com/<username>/<repo>.git

If I type:

git pull origin master

I get:

From https://github.com/<username>/<repo>
    * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

I have also tried:

git pull origin master --allow-unrelated-histories

But I get:

From https://github.com/...
    * branch            master     -> FETCH_HEAD
error: Your local changes to the following files would be overwritten by merge:
    <files>
Please commit your changes or stash them before you merge.
Aborting
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ramab01
  • 151
  • 1
  • 1
  • 5
  • Are you starting the new repository? – max630 May 01 '18 at 03:52
  • About the second error: https://stackoverflow.com/questions/35450049/what-does-would-be-overwritten-by-merge-mean – max630 May 01 '18 at 03:57
  • technically yes because it didn't find the repository that I was working . SO i typed this : ``` git remote add origin https://github.co/username/xxxxx.git ``` – ramab01 May 01 '18 at 04:28
  • Possible duplicate of [What does "would be overwritten by merge" mean?](https://stackoverflow.com/questions/35450049/what-does-would-be-overwritten-by-merge-mean) – phd May 01 '18 at 14:36
  • `git pull origin master --allow-unrelated-histories` .See here https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase – Bhargav Variya Mar 20 '21 at 06:48

1 Answers1

54

You need to either reset or commit your changes first:

git reset --hard

or:

git commit -m "saving changes..."

Then you can do:

git pull origin master --allow-unrelated-histories
Fernando Espinosa
  • 4,625
  • 1
  • 30
  • 37
  • 2
    I had the same problem and the command below solved it. `git pull origin master --allow-unrelated-histories` I created a github repo (with one default file commited) and after that I created a local repository. So, the repos (remote and local) started independently and with different contents. That´s why --allow-unrelated-histories is necessary. IMHO, this issues must be closed. – Cláudio Oct 15 '18 at 13:38
  • I have an annoying `fatal: refusing to merge unrelated histories` – Ced Sep 25 '19 at 14:52