How to merge branches with entirely different commit histories in Github?

Viewed 926

I wanted to give Github a try, so I create an account. To train myself, I created a remote repository, cloned into a folder on my computer, and experimented how to push a file I modified to the remote repository. For this, everything is fine.

Now comes the trouble: to experiment further, I created another remote repository, to which I want to push some code from a local repository. The push operation seems to be fine, as the pictures here below show

Picture 1

Picture 2

As you can see, the branch in my remote repository is called "main", and the branch in my local repository is called "master". The problem is when I try to make the changes effective in the remote repository (i.e. when I try to merge "master" into "main" by clicking the "Compare & pull request" button, if I express myself correctly ?), Github tells me that

There isn’t anything to compare. main and master are entirely different commit histories.

Picture 3

hence I cannot put the changes online. Can someone explain me how to solve that problem ?

N.B.: I use Git Bash to have a command line to execute the operations

2 Answers

Check the pulldown to switch branches in the remote repository, located on the left just above the code list. You should be able to toggle between the two, and see everything has been pushed into master. That said, I found your question as it is similar to mine... I have that same error "... main and master are entirely different commit histories" - and being new to doing GitHub right - was trying to (or think I should) pull or merge them together. I did read where 'Main' is supposed to replace 'Master', so it is confusing I have ended up with both. Old repos will continue to use master until GitHub announces a 'seamless' transition for them. I suspect in creating my local repo, I used old instructions from before main was in use, that may have added the master branch.

Looking around, I have managed to solve this for myself, and now have only the main branch, with master deleted safely. I put my steps below - please read the referenced links, FIRST backup your files, and enter at your own risk! It work for me is all I can say. Others more experienced using Git Bash may be able to offer advice on how to enhance or even remove some of the steps.

NOTE: My repo is only about a week old... so my tolerance for risk is pretty high!!

BACK UP YOUR REPO if you want to try this.

Inspired by meagar's response to a question on this, which, that solution did not work for me directly without modification/guidance from advice on GitHub replacing master with main here.

The steps were:

  1. I BACKED UP MY LOCAL REPO!! DO THIS! :)
  2. git branch showed only master; so I checked out and switch to main git checkout main
  3. I checked with git branch to see that both branches were now listed
  4. I did git status to see that 'Your branch is up to date with 'origin/main'
  5. ls confirmed I was in main, with just the README file, whereas master had many more files
  6. git fetch while on the main branch
  7. Then I performed meager's steps:
  8. I checked out the master, and 'Switched to branch with git checkout master 'master', "Your branch is up to date with 'origin/master' "
  9. Then git rebase origin/main which updated the header for master
  10. I added a git status for a reality check, Bash response: "On branch master. Your branch and origin/master have diverged... and some details.... (use "git pull" to merge the remote branch to yours) nothing to commit....
  11. I did the git pull as suggested and got "fatal: refusing to merge unrelated histories.
  12. So I did git fetch again then git branch to confirm both main and master were there, yes they are
  13. Then followed the second reference linked for this advice to add a flag: git pull origin master --allow-unrelated-histories.
  14. then git commit -m "merging main and master"
  15. then I went to the remote repo online to confirm/set main as the default branch
  16. I did git push then did an online repo pull request, confirmed which reported it was 'safe to delete master'
  17. Finally back in the bash, git push origin --delete master checking the remote repo, all is there.

Condensed steps without detail:

git branch
git checkout main
git branch
git status
ls
git fetch
git checkout master
git rebase origin/main
git status
git pull (fatal: refusing to merge unrelated histories)
git fetch
git branch
git pull origin master --allow-unrelated-histories
git commit -m "merging main and master"
[CHECK REMOTE REPO THAT MAIN IS DEFAULT BRANCH]
git push
[PULL REQUEST TO MERGE ON REMOTE REPO - MESSAGE "... safe to delete master"]
git push origin --delete master
Related