DEV Community

loading...

Fatal: refusing to merge unrelated histories Git unrelated-histories and local

Ajeeb.K.P
Muslim, Husband, Programmer, Indian/Kerala. Speaks English, Malayalam. Always curious about technology, computing etc.
・1 min read

Intro

This is a small learning about git.

  1. --allow-unrelated-histories
  2. local git copies

Unrelated-histories

Unrelated histories may happen sometimes. Usually, we use git init command and create a git repo. This has some unique identity (I didn't dig into internals, I'm just speculating).

So, how the unrelated histories happen ?

Here are few scenarios, I can think.

  • One personally git init locally, and done a fair amount of coding. Another person, created git repo just for the first person.
  • If one person shares a project folder to other without initializing a git repo (ie. git init). Then, both of them git init at later stage.

Then, you might end-up in unrelated histories. Probably, you will see this error.
fatal: refusing to merge unrelated histories.

The option to fix this is
--allow-unrelated-histories

For example, see following command
git pull origin master --allow-unrelated-histories https://github/username/reponame.git

local git copies

Say two developers are working on project and both are using git. They can merge git repos locally as well.

git pull /path/to/other-copy

Actually, other operations like clone, pull, etc. work as well.

Reference

https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase
https://mkyong.com/git/git-pull-refusing-to-merge-unrelated-histories/
https://www.datree.io/resources/git-error-fatal-refusing-to-merge-unrelated-histories

Discussion (2)

Collapse
waylonwalker profile image
Waylon Walker

I've ran into this a number of times as well. I think you can run into this issue for anything that changes history, such as a rebase. I think its generally ok to git push --allow-unrelated-histories to your own repo with no collaborators, or to your own branch, steer clear with a branch that has collaborators.

Collapse
ajeebkp23 profile image
Ajeeb.K.P Author

Thanks for sharing your experience and thoughts.