16

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

sk8forether
  • 247
  • 2
  • 9
django_moose
  • 355
  • 1
  • 4
  • 10
  • 1
    You could try creating a branch from `master` named `branch_x2`, and then manually bringing your changes from `branch_x` into your new `branch_x2`. You would then have a branch with a common ancestor of `master` that you could try merging back in. – Adil B Aug 21 '18 at 19:23
  • Would I have to delete the fork? Because I thought I could do all of that with the fork, excluding the new branch ("branch_x2"). – django_moose Aug 21 '18 at 19:53
  • 1
    You should create the new `branch_x2` on your fork, replicate your `branch_x` changes in `branch_x2`, and then make a pull request from `yourfork:branch_x2` to `upstream:master`. So yes, you should be able to do all of this within your fork. – Adil B Aug 21 '18 at 20:11
  • Thanks, that does make sense. However, I don't know how to replicate branch_x changes in branch_x2. Is this using merge? I am still new to Git – django_moose Aug 21 '18 at 20:30
  • 1
    No worries! First, make sure you've got `branch_x2` checked out: `git checkout -b branch_x2`. Then, copy the files from `branch_x` using regular Windows copy commands and paste/overwrite the files in your fork where you have `branch_x2` checked out. Running `git status` should then show you all of your changes from `branch_x`, but now in `branch_x2`. – Adil B Aug 21 '18 at 21:05

3 Answers3

19

I found this to work, the --allow-unrelated-histories flag

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

you should now be able to compare and PR

PhilCowan
  • 523
  • 5
  • 13
13

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

testing_22
  • 2,340
  • 1
  • 12
  • 28
Philippe
  • 28,207
  • 6
  • 54
  • 78
  • Old commits on the checked-out branch (not the reset one) seem to have become disassociated with the branch, though their contents remained. I found this result strange, but I was able to complete the PR successfully. – sk8forether Feb 17 '21 at 22:20
  • if you're resorting to `--force` you're doing it wrong. that is most likely not needed for these purposes unless you're trying to augment git history. this answer will most likely work for you: https://stackoverflow.com/a/63603128/349100 – Sonic Soul Oct 30 '21 at 18:27
  • @Sonic-soul the 2 solutions don't express the same thing and as the original question was more about fixing an error, my solution is better because you end up in the exact same state than if you didn't do the error. And doing a '--force' to have a better history sometimes outweigh the disadvantages. And your 'augment git history' means nothing. – Philippe Oct 30 '21 at 22:10
0

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.

django_moose
  • 355
  • 1
  • 4
  • 10
  • 1
    This is kind of a manual work around that allows you to have a related history. while it does solve the problem, it doesn't really answer your original question. – PhilCowan Aug 10 '21 at 17:26