The “refusing to merge unrelated histories” message means that your local and remote repositories have absolutely no commits in common. That can mean a few things:
- You have the wrong remote repository (seems unlikely if it’s already configured as
- You edited the commit history back to the first commit (
git commit --amend, or maybe
git filter-branch), so even if the content is the same the commits are all different.
- It could also be that the remote repository was initialized with a commit (e.g. a default readme), but you don’t actually care about that.
I’ll assume the second case here, but the third one can be handled the same way.
git fetch origin to have the current state of the remote repository available to compare.
- Check the commit logs of both the local and remote branches (
git log master,
git log origin/master).
- You can also use
git diff origin/master..master to see what your local branch changes relative to the remote one.
Decide what you want to keep. That can be just one version (I’d guess the local one). If you also want to keep a few commits from remote you can use
git cherry-pick to add them to the local branch. You could also create a new branch based on whichever branch most fits what you want to keep and then cherry pick, to have a sort of sandbox where you won’t destroy anything.
Once you have a branch that you are absolutely sure contains what you want to keep, you can force-push (
git push --force) it to the remote repository. You have to be absolutely sure, because this will DESTROY EVERYTHING on the remote branch.
Theoretically you can also tell git to merge despite the unconnected histories, but that is almost always the wrong choice (which is why git refuses do to it by default).