How to resolve the Git error "refusing to merge unrelated histories"

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


When using Git, you may encounter the error message: refusing to merge unrelated histories. This error typically occurs when trying to merge two repositories that do not share a common history. This guide provides a detailed explanation of why this error occurs and how to resolve it effectively.

The refusing to merge unrelated histories error happens when you attempt to merge branches or repositories that Git perceives as having no common commit history. This situation often arises in scenarios such as:

  • Merging an initial commit of a new repository with an existing one.
  • Combining two completely separate repositories.
  • Pulling changes from a remote repository into a new local repository.

To resolve the refusing to merge unrelated histories error, you need to instruct Git to allow merging of unrelated histories. This can be done using the --allow-unrelated-histories flag.

Let’s consider a scenario where you want to merge changes from a remote repository into your local repository, but Git throws the error due to unrelated histories.

  1. Clone or initialize your repositories:

    If you haven't already, clone the remote repository and navigate to your local repository directory. Here are the commands for cloning and initializing:

    Terminal
    git clone <remote-repo-url> remote-repo
    cd remote-repo

    Or initialize a new local repository if you don't have one:

    Terminal
    mkdir local-repo
    cd local-repo
    git init
  2. Add the remote repository:

    Add the remote repository to your local repository as a new remote:

    Terminal
    git remote add origin <remote-repo-url>
  3. Fetch the changes from the remote repository:

    Fetch the changes from the remote repository:

    Terminal
    git fetch origin
  4. Merge the remote branch with the --allow-unrelated-histories flag:

    Attempt to merge the remote branch (e.g., main) into your local branch (e.g., main) using the --allow-unrelated-histories flag:

    Terminal
    git merge origin/main --allow-unrelated-histories

    This command tells Git to permit the merge despite the lack of shared history between the branches.

  5. Resolve any conflicts:

    If there are any merge conflicts, Git will prompt you to resolve them. Open the conflicted files, resolve the conflicts, and then mark the files as resolved:

    Terminal
    git add <file-with-conflict>
  6. Commit the merge:

    After resolving conflicts, commit the merge:

    Terminal
    git commit

    Provide a commit message describing the merge and conflict resolution.

  7. Push the changes to the remote repository:

    Finally, push the merged changes back to the remote repository:

    Terminal
    git push origin main
  • Merging two completely separate repositories:

    If you are combining two distinct repositories, follow similar steps but ensure that you add both repositories as remotes and fetch changes from each. Use the --allow-unrelated-histories flag when merging their branches.

  • Pulling changes into a new local repository:

    If you have a new local repository and want to pull changes from an existing remote repository, use the --allow-unrelated-histories flag during the pull operation:

    Terminal
    git pull origin main --allow-unrelated-histories

If after following these steps and you are still seeing the Git error refusing to merge unrelated histories, see the official Git documentation for more details.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2