How to "add" the history of a previous git repo to a new one?

So, I had this repo on a GitHub account, and also stored in local on the PC. At a certain point I deleted the account and made a new one. I created a blank repo on the new one, picked the local files from the old repo (minus the .git folder) and put them in the new repo and published it. From then on I continued the development on the new acc, making commits etc.. Now, considering that I still am in possess of the original local files of the repo that was in the older account, is there a way to "rewrite" the git history of the new repo to add the commits from the old one (and possibly also add the eventual branches, pull requests etc.)? I guess this is something to be done with git rebase but I don't know precisely what to do.

I searched online, found something related but not specifically for this scenario and I'm afraid that I'll do a mess. What's the right way to do this?


Consider I have a github repo containing the OLD repo on main branch.

I managed to put the old and the new repo together by following these steps:

  1. Add a branch to the OLD repo called "old-repo-new-branch";
  2. In the newly created branch, push the NEW repo;
  3. Run this command git rebase --onto IdOfLastCommitInMainBranch IdOfFirstCommitInNewBranch old-repo-new-branch (swap IdOfLastCommitInMainBranch and IdOfFirstCommitInNewBranch with the relative ids);
  4. It rebased and put together the 2 repos in the old-repo-new-branch branch, with their commit histories, but then when syncing (pull & push) with the remote I got this new error: fatal: refusing to merge unrelated histories;
  5. I googled it up and solved by doing git pull origin old-repo-new-branch --allow-unrelated-histories and then pushing;

Now the issue is that the commit in the "middle" of the 2 histories (old and new) looks to not have a parent (it should have the penultimate OLD repo commit as parent). In fact when I look it up the diff on or GitHub Desktop, it has all the files in the repo with a green "+". Instead it should show the modifies in respect to the previous commit.


I was doing a mess cause I was rebasing while the repo was already pushed to github. I followed @etfshift0 answer and used: git rebase --onto id-of-last-commit-to-keep-from-old-repo id-of-first-commit-in-new-repo branch-of-new-repo while having the newRepo branch only locally, and then published it. Solved!


  • It can be done easily if the tree of the first commit of the new repo is exactly like the tree of a given commit from the old repo. If you can get a commit from the old repo that has a tree that is exactly the same as the tree of the first commit of the new repo, then you can use a script I developed for exactly this purpose:

    You can use it in a repo that can see the old and the new history (that is a separate question if you don't know how to do that).

    If the trees are not the same then you need to find the commit from the old repo that has a tree that best resembles the tree in the first commit of the new repo... Let's call that commit from the old repo X. Then you can do this:

    git rebase --onto X id-of-first-commit-in-new-repo the-branch-of-the-new-repo

    That should place all commits after the first commit from the new repo on top of the old branch.

    I would advice to create a temporary branch at the same place of the new repo branch and play with it until you get the results you want so not to mess up the real branch.

    Assumption: I am assuming that the first commit of the new repo was just copying the files from the old repo and committing without adding any changes.