Search code examples
githubgithub-for-windows

How to correct «unable to merge unrelated histories»?


I use github desktop (https://desktop.github.com ) while developing the application with several other people, so, for some reason, when trying to merge two branches into one, the error "unable to merge unrelated histories" is displayed for one of the target branches.

What could be the problem?


Solution

  • First of all: You may also be able to find a solution using the search.

    Potential reasons for the error message

    From: https://komodor.com/learn/how-to-fix-fatal-refusing-to-merge-unrelated-histories-error

    Here are some common scenarios where fatal: refusing to merge unrelated histories can occur.

    1. You have a new Git repository with some commits. You then try to pull from an existing remote repo. The merge becomes incompatible because the histories for branch and remote pull are different. Git sees the situation as you trying to merge two completely unrelated branches, and it doesn’t know what to do.
    2. There’s something wrong with the .git directory. It may have been accidentally deleted at some point or got corrupted. This can happen if you’ve cloned or cleaned a project. Here the error occurs because Git doesn’t have the necessary information about your local project’s history.
    3. The branches are at different HEAD positions when you try to push or pull data from a remote repo and cannot be matched due to a lack of commonality.

    Options to resolve the issue

    The article describes two options on how to resolve/avoid such issues but targets command line /terminal users. I guess I would prefer option 2 over option 1 anyway, also using git in the terminal.

    The article explains it like this:

    The alternative (and longer) way of fixing refusing to merge unrelated histories issues is to unstage your current commits, stash them, clone your required remote repository, and then place your stashed branch contents into the new clone. This will ensure that any conflicts that you may encounter in the code are addressed before merging and prevent application errors from occurring.

    How it (should) work in GitHub Desktop

    In GitHub Desktop you should be able to use a modified version of option 2:

    1. To unstage all the files in your last commit, double click staged files. This moves them to the unstaged area. Learn more in this GitHub issue.
    2. To stash your unsaved files, right-click an unstaged file. Learn more about stashing files.

    This will give you a clean working tree to pull your remote repository into. Once you’ve successfully pulled into your branch, you can:

    • unstash your files (see link above again) to reapply them to your current working copy.
    • commit them as a separate commit.
    • resolve any file conflicts that you may have.

    I hope this explanation adds some clarity. Let me know if there are any wrong or misleading information in my text please.