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?
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?
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
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
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.
git pull origin master --allow-unrelated-histories
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
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.
sample
in remoteold-sample
sample
in remoteThe 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.