You can use --allow-unrelated-histories to force the merge to happen.
The reason behind this is that 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-historiesoption 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.
More information can be found in this answer.
Answer from blue112 on Stack OverflowYou can use --allow-unrelated-histories to force the merge to happen.
The reason behind this is that 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-historiesoption 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.
More information can be found in this answer.
In my case, the error was just fatal: refusing to merge unrelated histories on every try, especially the first pull request after remotely adding a Git repository.
Using the --allow-unrelated-histories flag worked with a pull request in this way:
git pull origin branchname --allow-unrelated-histories
As per 2.9.0 release notes - git pull has been taught to pass the --allow-unrelated-histories option to underlying git merge
Gitlab runner 13.1.1 checks out a commit and not my branch
How am I supposed to merge these two? when I try to git merge master, I get told "refusing to merge unrelated histories"
How to deal with "refusing to merge unrelated histories" error
github - Git failed with a fatal error, refusing to merge unrelated histories with Git push - Stack Overflow
How to Fix "fatal: refusing to merge unrelated histories" in Git
How can I prevent 'fatal: refusing to merge unrelated histories' errors?
What happens if I merge unrelated histories?
Basic tip: pull from clean source, copy changed files, etc.
In GitLab you have to unprotect your master branch (in settings → repository)
Also if your Git is too old, the --allow-unrelated-histories might not be implemented. You could do the push --force origin master, but remember that it might mess up not even your repository. Also all remote clients might have to pull --force or clone again.
See in (long discussion) Git refusing to merge unrelated histories on rebase.
The --allow-unrelated-histories flag applies only to merging. Since git push does not merge (ever), it has no --allow-unrelated-histories option. (Contrast this with the git pull command, which does sometimes—well, quite often, really—run git merge.)
I tried changing the username of all commits ...
You cannot change anything about any existing commit. All commits are 100% read-only.
What you can do is take a series of commits that have something about them that you don't like—such as the name and/or email address of the author and/or committer—and copy them to new and improved commits that are otherwise the same, but have this issue corrected. It appears that this must be what you did, because:
... [after] the pull command ... every commit is simply doubled with the correct and incorrect username.
This doubling is, of course, exactly what you did when you made corrected commits. You told your Git to discard the old (incorrect) commits in favor of the new improved ones, and it did. But another Git repository—the one over on GitHub—still had, and has, the incorrect commits. You then told your Git to obtain any commits they have, that you don't—which in this case were the commits you'd just discarded—and then join them up with the commits you have that they don't, which in this case are your new-and-improved replacements.
You must discard the joining-up merge, typically via git reset. This gets you back to the situation in which you have replaced your old-and-incorrect commits with your new-and-improved copies—i.e., back to where you were before you ran git pull. You are now back where you were: you still need to convince the Git over at GitHub to discard its old-and-incorrect commits in favor of your new-and-improved ones.
To do that, you must use git push --force or git push --force-with-lease. You must also have permission to do this, though if the GitHub repository is under your control, this is either already the case, or easy to set up (you have to tell GitHub what to forbid, so if you did not forbid yourself from force-push earlier, you need not change anything now).
Note that discarding any given commit necessarily discards all subsequent commits. So if there are additional commits you'd like to keep, you must first copy those to new-and-improved commits, whose improvement is that they append to your existing new-and-improved commits.