Git 'fatal: refusing to merge unrelated histories': how do you fix it?
The two branches you're trying to merge share no common commit ancestor, so Git refuses rather than stitch together histories that may have nothing to do with each other. Override it with git pull origin main --allow-unrelated-histories, or, for a plain merge, git merge <branch> --allow-unrelated-histories. That flag tells Git you know the two histories are independent and you intend to join them anyway; it will create a merge commit tying both roots together. The near-universal trigger: you ran git init and committed locally, then added a remote and pulled from it, and that remote already had its own commit, most often a README or license that GitHub auto-created when you made the repo. Two separate initial commits, no shared root, so Git balks. Only use the flag if you genuinely mean to combine the two histories; if instead you meant to start from the remote, the cleaner move is to clone it fresh and copy your files in.
Why Git blocks this by default
Git 2.9, released in June 2016, changed the default so that git merge and git pull refuse to combine two histories with no common ancestor unless you pass --allow-unrelated-histories. Before that, Git would merge them silently. The guard exists to catch a specific mistake: pulling from the wrong repository, or pointing a remote at a project that isn't actually the upstream of what you have locally. Without the check, that fat-finger would fold an unrelated project's entire history into yours and leave a confusing merge to untangle. The refusal is Git asking you to confirm the two roots really belong together.
The fix
# joining your local commits with a remote that already has its own
git pull origin main --allow-unrelated-histories
# resolve any conflicts, then commit the merge
git add .
git commit
After the pull, you may get merge conflicts where the two histories touched the same files (a local README versus GitHub's generated one is the classic case). Resolve them normally, then commit. If you didn't actually want to merge, don't reach for the flag: clone the remote into a new directory, move your working files over, and commit them on top of the remote's history instead. That avoids a merge commit joining two unrelated roots you never meant to link.
Notes from fernforge. Default behavior per the git-merge documentation; the refusal became the default in Git 2.9.