86

In my local, I made new text file -> git add newfile.txt -> commit -> pull origin master -> ERROR!

"refusing to merge unrelated histories".

What is unrelated histories? , what is related histories?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Byeongin Yoon
  • 2,307
  • 4
  • 21
  • 38
  • What did you do to setup your local repository? Did you run `git init` + `git remote add ...`? What is the remote and what are you trying to do? – max630 Jul 24 '17 at 04:46
  • 4
    A branch points to a commit. A non-root commit has one or more parents and each parent commit has its own one or more parents. When two branches don't have any common ancestor in their histories, they are unrelated. – ElpieKay Jul 24 '17 at 05:00
  • Thank you for your comments! I just wanted to know what 'unrelated history' not to resolve my problem. – Byeongin Yoon Jul 24 '17 at 05:23
  • 1
    Possible duplicate of [Git refusing to merge unrelated histories](https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories) – Natesh bhat Oct 18 '17 at 10:27
  • 1
    There is a better and more inclusive answer here https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase – Abednego Jun 28 '20 at 22:49
  • `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:47

5 Answers5

151

I think you have commit in remote repository and when you pull this error happen.

use this command

git pull origin master --allow-unrelated-histories
git merge origin origin/master
Yashar Panahi
  • 2,144
  • 1
  • 15
  • 21
  • 12
    i suggest reading at https://stackoverflow.com/questions/39761024/refusing-to-merge-unrelated-histories-failure-while-pulling-to-recovered-repos – jitter Nov 16 '17 at 10:11
  • 24
    This solution is helpful, but note that the OP actually wanted ***understanding*** and not a fix. Therefore, the comments threaded in the question above are actually more helpful. (I had the same issue; I knew how to fix it, but never really knew what "it" was... I didn't understand what "unrelated histories" actually meant.) – Mike Williamson Apr 27 '18 at 21:57
  • 1
    The link that @jitter points to gives a perfect & well-detailed answer. – Mike Williamson Apr 27 '18 at 21:58
  • This worked when I had to create a new GitHub repo with License commit and merge with my project that was already old. I guess the best solution is to push first, then add other stuff. But in case you make my mistake, anyone know how to do this correctly? – gagarwa Feb 11 '19 at 03:11
  • 1
    This is a nice answer but for more details you can check out this question https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase – Abednego Jun 28 '20 at 22:50
  • I may be wrong. But I think your second command should be `git merge origin/master` – Tanzeel Dec 29 '20 at 03:47
  • If you get `fatal: couldn't find remote ref master`, try it with "main" instead of "master", since your branch might perhaps not be called like this. – questionto42standswithUkraine Jun 18 '21 at 18:52
14

I ran into a similar problem where I brought in a branch from a second remote and wanted to merge with a branch from the first remote. This is different from the existing answers as I'm not using --allow-unrelated-histories on the pull, but on the merge.

git checkout master
git merge --allow-unrelated-histories myfunnybrancy
Brian C.
  • 4,820
  • 2
  • 28
  • 38
  • 2
    This is exactly what I needed. I had a repo created for me with a develop branch (with a gitignore in it). My local repo was a master branch. I set the remote origin and then pushed my master branch. I then wanted to merge the master into the nearly empty develop so they'd be in sync for further development. This did the trick. – P. Ent Jun 28 '21 at 19:10
12

When somehow the local .git subdirectory is lost, the whole project seems to be appeared from nowhere, as all the local changes histories were contained by .git. Thus, your local changes become unrelated. That is why all the changes are called unrelated histories then.

In this situation, git merge or pull request will unable to track where you made changes to add with the remote project. Hence, " refusing to merge unrelated histories"- error occurs.

In this situation, if you try to force merge by following commands,

git pull origin master --allow-unrelated-histories

git merge origin origin/master

it will create a lot of conflicts, as it is not able to find the history of your local changes.

Tasnim Fabiha
  • 1,114
  • 1
  • 17
  • 31
1

git pull origin master --allow-unrelated-histories

0

I got this issue when renaming one of repository in GitHub Enterprise, and then making same former named repo as below steps.

  1. create repo sample in remote
  2. clone it to local
  3. rename remote to old-sample
  4. create a new repo named sample in remote
  5. try to push it from local
  6. error occurred.

Weird thing is that I removed local repo but git tries to clone old one even if I try to clone by newly created repo url. The clone url is just automatically redirected to old one even if the new one is existed in remote. I don't know this cause is from server or local git process.

For this reason, the error occurs because the git process fails to compare its local commit history to remote history.

The unrelated histories could tell in above situation.

I ended up to remove renamed old repo in remote and it solved.

Youngjae
  • 22,962
  • 17
  • 106
  • 189