0

In our project, using master and develop branches, a commit has been made directly on master (only three files, and those changes are also on develop branch).

Now I need to merge develop to master, but I cannot since the history for master is now "disjoint" from develop and I get the following error: fatal: refusing to merge unrelated histories.

I made a reset commit hard on master as I do not want to retain locally the changes of the last commit as those are already in develop. However after the revert the local master branch is 600 changes ahead and 1066 behind.

Why such a number of changes? I would expect that the local master branch is not behind at all, as the revert moves the pointer to the previous commit.

Does it count all the previous push/pull made on master branch since the beginning (by doing pull I still get the error fatal: refusing to merge unrelated histories)?

The expected/desired result would be to go back of one commit on master, when this branch was still on the history line of develop, allowing me to merge the two. enter image description here

Francesco
  • 9,947
  • 7
  • 67
  • 110
  • 3
    **Reset** moves the pointer, **revert** creates a new commit. Which one did you do? – 1615903 Mar 09 '17 at 10:33
  • I made a revert ("revert current branch to this commit" using sourceTree) – Francesco Mar 09 '17 at 10:36
  • Could you show a screenshot of the commit graph shown by SourceTree? – mkrieger1 Mar 09 '17 at 10:39
  • Which Git version are you using? – mkrieger1 Mar 09 '17 at 10:40
  • 1
    The "unrelated histories" means that your current branch and the other branch have no commits in common at all. Typically this is the result of combining unrelated repositories, or running `git filter-branch` all the way to the root commit. – torek Mar 09 '17 at 10:46
  • Added the screenshot. Using "git version 2.10.1.windows.1" – Francesco Mar 09 '17 at 10:49
  • Where are the `develop` and `master` branch pointers? There is only `origin/master` shown. – mkrieger1 Mar 09 '17 at 11:11
  • Develop is the blue one on the far left. The graphic would have been to big if I would have included all. As showed in the screenshot I cannot merge develop in master as the latter has an independent commit. – Francesco Mar 09 '17 at 12:09
  • Normally you should be able to merge a diverged branch into another. That's the point of a merge. There must have been something else that went "wrong" in your case, which we cannot see here. – mkrieger1 Mar 09 '17 at 13:44
  • Possible duplicate of ["refusing to merge unrelated histories" failure while pulling to recovered repository](http://stackoverflow.com/questions/39761024/refusing-to-merge-unrelated-histories-failure-while-pulling-to-recovered-repos) – mkrieger1 Mar 09 '17 at 13:44
  • What was the exact command you used when you say "I made a `revert commit hard` on master"? Did you actually mean `reset`, not `revert`? It may be that you've moved your (local) `master` branch pointer to a commit entirely unrelated to `develop` or `origin/master`, which could explain why Git refused to do the merge. Even though you have now found the magic command line argument to allow the merge, I would be suspicious that this got you in the situation you actually want. – mkrieger1 Mar 09 '17 at 13:49
  • Yep, it was likely "reset", not "revert", comparing the menu items shown in the first screenshot from [here](https://confluence.atlassian.com/sourcetreekb/reset-branch-to-a-commit-788730897.html) with your first comment above. – mkrieger1 Mar 09 '17 at 13:52
  • @mkrieger1 : yes, I used that command from source tree, but then I went back and recreated a the repo locally. I will update my question. The problem was due that a commit was done on master directly and this make it diverge from develop. I had to use the merge --allow-unrelated-histories command to solve it, not using reset anymore. Anyone can explain why by using reset did I get so many differences (ahead/behind)? – Francesco Mar 09 '17 at 14:39

1 Answers1

0

I was able to solve the issue by using the command:

git merge develop --allow-unrelated-histories

This "flattened" master branch, allowing me to merge develop branch into master.

Francesco
  • 9,947
  • 7
  • 67
  • 110