'How to do a pull request when my branch and master have no related history?

I have a branch, "branch_x", that is part of the same repository as master. However, I foolishly started developing on branch_x without checking out from master. Therefore there is no common ancestor between master and branch_x, and when I tried doing a pull request to master, I simply got: There isn’t anything to compare. master and branch_x are entirely different commit histories.

Because of that fault, I made a fork of that repository. I am trying to merge the commits of branch_x into the master branch of the fork, and then I will try to request a pull between original repository and the fork (because now they have some history in common). However, I have no idea of how to actually merge branch_x from the original repo, to the master branch of the fork.

This post is similar, but I don't think that "git rebase -onto" will help much: There isn't anything to compare. Nothing to compare, branches are entirely different commit histories



Solution 1:[1]

As seen in @Adil B comment, the answer to this solution was to make the new branch_x2, within the fork. Then, copy and replace the files representing the most recent commit in branch_x of the original repo, to branch_x2. You will now have a related history, and the files you want to compare. You can be on either branch (when in the fork) and do the pull request, as long as that branch represents the commit and files/directories that you need.

Solution 2:[2]

I found this to work, do your normal flow from the branch_x that you want to push up.

git add .
git commit -m "message"
git pull origin master(or whatever branch) --allow-unrelated-histories
git push origin branch_x

you should now be able to compare and PR

Solution 3:[3]

This is easily doable by doing the following:

  1. Checkout branch_x to work on it:

    git checkout branch_x

  2. Reset soft to master so that branch_x is now at the same place than master in the git history but all your changes are now staged (reset --soft doesn't touch files in the working directory and even add the changes directly to the staging area):

    git reset --soft master

  3. Commit the files so that it will create one commit containing just the changes made in branch_x compared to master

    git commit

And now branch_x is one commit ahead of master and you can create your PR. You perhaps will have to push --force depending if you already pushed branch_x

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 django_moose
Solution 2 developerick
Solution 3 testing_22