128

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

"refusing to merge unrelated histories".

What are unrelated histories?

What are related histories?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Byeongin Yoon
  • 3,233
  • 6
  • 26
  • 44
  • 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
  • 6
    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
  • 2
    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

6 Answers6

217

I think you have committed in the remote repository, and when you pull, this error happens.

Use this command:

git pull origin master --allow-unrelated-histories
git merge origin origin/master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yashar Panahi
  • 2,816
  • 1
  • 16
  • 22
  • 14
    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
  • 29
    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
  • 2
    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
  • 2
    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. – questionto42 Jun 18 '21 at 18:52
  • It's incredible how many git commands/subcommands exist — for each and every tiny little problem, there is _always_ a not-very-well-documented git functionality that can be activated to "fix everything". No wonder people stick with Git and give up on the other alternatives (FOSS or commercial)... – Gwyneth Llewelyn Aug 01 '23 at 14:24
31

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.
  • 6,455
  • 3
  • 32
  • 42
  • 4
    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
  • ```l$ git merge --allow-unrelated-hisotries module-2 error: unknown option `allow-unrelated-hisotries' usage: git merge [] [...] or: git merge --abort or: git merge --continue``` – mLstudent33 Jul 08 '23 at 05:15
15

When somehow the local .git subdirectory is lost, the whole project seems to appear 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tasnim Fabiha
  • 1,148
  • 1
  • 17
  • 31
7
git pull origin master --allow-unrelated-histories
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
2

Just use the one line below: You do not need to specify origin and master and all that. Atleast not in a recent version of git. I have used this command and I may have lost a small amount of code but it was nothing major for me to recode. Use at your own risk. The problem is you lose your newest code still on your PC so after running this code copy and paste the actual lines of code not just the files and do a commit/push and everything should look fine. I am by no means an git expert but when you can no longer push or pull you can use this command or recreate your git repo with your latest code you get manage to get your hands on.

git pull --allow-unrelated-histories
bauderr
  • 47
  • 10
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – DSDmark Jun 27 '23 at 15:20
1

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

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

The weird thing is that I removed the local repository, but git tries to clone old one even if I try to clone by newly created repository URL. The clone URL is just automatically redirected to the old one, even if the new one exists in remote. I don't know if this is due to the server or local Git process.

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

The unrelated histories could tell in the above situation.

I ended up removing and renamed the old repository in remote and it was solved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Youngjae
  • 24,352
  • 18
  • 113
  • 198