The Localhost Blog
Modern web development guides hints and tips.
Git ` — allow-unrelated-histories`
How to combine two separate unrelated Git repositories into one with single history timeline.
Just adding a quick note on this…
I had a project that started off as it’s own project but it ended up
needing to go into an already existing project, so I thought ok add it
as a new branch on the existing project with
git remote add origin
then push the changes up to the origin then all is well with the
world. That was until I tried to compare the branches…
There isn’t anything to compare? Bit misleading there as there is but the commit histories are different. How do you get around it, well the first Google result was a bit sparse with the details but then I found this which was more helpful. I needed to merge the disparate branches which is now disabled by default in git but can be enabled with the —allow-unrelated-histories flag.
1git merge origin use-ts-bot --allow-unrelated-histories
This brought up, understandably, a few conflicts with the branch I wanted to merge with the origin.
1Auto-merging package.json2CONFLICT (add/add): Merge conflict in package.json3Auto-merging package-lock.json4CONFLICT (add/add): Merge conflict in package-lock.json5Auto-merging README.md6CONFLICT (add/add): Merge conflict in README.md7Auto-merging .gitignore8CONFLICT (add/add): Merge conflict in .gitignore9Automatic merge failed; fix conflicts and then commit the result.
To get around this I just took the latest version of the
branch from my GitHub account and then and used that in place of the
origin conflicts as what I’m working on will ultimately replace the
master branch, so it was just a copy pasta into my VSCode then
recommit the changes files. Phew! I now have a branch I can compare
with the master that I will one day make a PR for 😅