1

I have two branches in my repository: master and devel. I tried to merge devel into master but got the error fatal: refusing to merge unrelated histories.

On github, the master branch contains some files that I do not care about and do not need to merge. I do not need to keep the history of the master branch, I simply want to replace master with what is on devel.

Once I get devel updated to master, I need to tell my partner how to pull master without merging into his old local copy of master.

What I am considering doing is using advice from this post and running:

git branch -m master old-master
git branch -m devel master
git push -f origin master
git branch -d old-master

Do you think this will work? What should I tell my partner in order for him to get the latest copy of master once this is done?

Community
  • 1
  • 1
Casey
  • 2,611
  • 6
  • 34
  • 60

2 Answers2

1

The default behavior has changed since git 2.9:

"git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch --allow-unrelated-histories option to be used in a rare event that merges histories of two projects that started their lives independently.

See the git release changelog for more information.

You can use --allow-unrelated-histories to force the merge to happen.

Example: git pull origin branchname --allow-unrelated-histories

Community
  • 1
  • 1
vpdeva
  • 303
  • 2
  • 11
1

You got into quite a messy situation here.
Assuming your branch replacement went well you need to ask your partner if he have changes he would like to commit no his side.

  • If no changes on his side I would recommend cloning the remote repository and starting from fresh.
  • If your partner has changes I would recommend making a copy of the locale branch and cherry picking the changes.

Partner:

git checkout –b master_tmp master
git checkout master_tmp
git branch –D master
git fecth
git checkout master
#Locate the changes you are missing 
git log  master_tmp
git cherry pick sha1x…sha1y # look at the documentation of cherry pick.
#Resolve conflicts as usual  
git commit 
git push
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • The branch replacement went well. My partner does not have commits from his side. What is wrong with him doing git fetch, then git reset --hard origin/master? – Casey Apr 06 '17 at 03:42
  • You can try it but why not play it safe and avoid some later unexpected voodoo. you can just delete master with -D and checkout if you prefer not to clone. – Haim Raman Apr 06 '17 at 03:50
  • so run git branch -D master, then git fetch, git checkout master? – Casey Apr 06 '17 at 04:11