Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to pull files from my Github repository: "refusing to merge unrelated histories"

People also ask

What is refusing to merge unrelated histories?

0. The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged, and two projects are unaware of each other existence and having mismatch commit histories.

How do I fix unrelated branches in git?

But this doesn't mean that Git cannot perform any merge. In fact, all you need to do to merge unrelated branches is to use the flag --allow-unrelated-histories . This tells Git to combine all the files and commits of both unrelated branches into one branch, as long as there are no file conflicts.

How do I merge two GitHub branches?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.

What does git rebase do?

What is git rebase? From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base.


Try --allow-unrelated-histories

Like max630 commented, or as explained here Git refusing to merge unrelated histories


git checkout master
git merge origin/master --allow-unrelated-histories

Resolve conflict, then

git add -A .
git commit -m "Upload"
git push

While I'm all for unblocking people's work issues, I don't think "push --force" or "--allow_unrelated_histories" should be taught to new users as general solutions because they can cause real havoc to a repository when one uses them without understand why things aren't working in the first place.

When you have a situation like this where you started with a local repository, and want to make a remote on GitHub to share your work with, there is something to watch out for.

When you create the new online repository, there's an option "Initialize this repository with a README". If you read the fine print, it says "Skip this step if you’re importing an existing repository."

You may have checked that box. Or similarly, you made an add/commit online before you attempted an initial push. What happens is you create a unique commit history in each place and they can't be reconciled without the special allowance mentioned in Nevermore's answer (because git doesn't want you to operate that way). You can follow some of the advice mentioned here, or more simply just don't check that option next time you want to link some local files to a brand new remote; keeping the remote clean for that initial push.

Reference: my first experience with git + hub was to run into this same problem and do a lot of learning to understand what had happened and why.


On your branch - say master, pull and allow unrelated histories

git pull origin master --allow-unrelated-histories

Worked for me.