38
$ git pull origin master
From https://bitbucket.org/tecgenome/chl-v2.0-html
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

How can I avoid or get past that error message?

Mukesh Rawat
  • 894
  • 2
  • 8
  • 27

3 Answers3

84

Since Git 2.9 (April 2016), you can try:

git pull --allow-unrelated-histories origin master

But check why those branches are no longer common though.
Maybe there was a force push rewriting all the history of origin/master.

In which case, if you don't have local commits of your own, it is best to reset your branch to the new one:

Warning: this will delete any untracked file, and reset your repository to origin/master
(You can try it in a copy of your current local clone)

git fetch

# Warning: RESET AHEAD
git reset --hard origin/master
Pang
  • 9,564
  • 146
  • 81
  • 122
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Doesn't fix the problem. Not sure what `pull` has to do with a merge problem firstly and the second solution you give still doesn't fix the merge problem. – geoidesic Aug 16 '20 at 14:28
  • I used Knight2016's solution. – geoidesic Oct 13 '20 at 15:43
  • not blaming the person who answered just sharing my experience, i attempted this solution and it deleted almost all of my local work, thankfully no harm done, however for those who don't know *this will delete local work which isnt in the remote git repo* – Liam O'Toole Jul 07 '21 at 18:36
  • 1
    @LiamO'Toole I am sorry to read that. I have edited the answer to better highlight the "reset" effect and its danger. – VonC Jul 07 '21 at 19:57
15

I did meet the same issue, and try the command it gets work.

git merge abbranch --allow-unrelated-histories

here we assume that abbranch is unrelated to current branch. Above command merge abbranch to current branch.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
knight2016
  • 539
  • 6
  • 12
  • 1
    In may case, and using the principle @knight206 points to, `git merge origin/master --allow-unrelated-histories` worked. – John Bentley Mar 23 '22 at 04:35
-1

if you are facing push Recjected or this issue in android studio you just open your project enable version control integration

and Move to project from android. right click on package --> go to Git --> Add

now come on downside click on terminal and follow given below step

 git remote add origin <repository url>

    git clone <repository url>
    git pull origin master --allow-unrelated-histories
    git merge origin origin/master
    ... add and commit here...
    git push origin master