Search code examples
gitgit-rebasegit-rewrite-historygit-cherry-pick

Separating two Git repositories that were merged together


I have a project I've been working on in Visual Studio for some time, and been saving my changes in a Git repository. Let's call this Project A, which has the following commit history

A0-A1-A2-A3-A4-A5-A6

Later I start working on another project Project B, which is in another Git repository.

B0-B1-B2-B3

At some point I decided that these two projects are really directly related to each other and I merge Project B into the Project A repository and add it to the Project A Visual Studio solution. The Project A repo now has two root commits A0 and B0.

A0-A1-A2-A3-A4-A5-A6-M1
                     /
        B0-B1-B2-B3-

After this point I assume these projects are one and don't bother separating my work into independent Project A and Project B branches. Most of the commits are related to either Project A or Project B. Although, there are some commits where I made changed to both A and B.

A0-A1-A2-A3-A4-A5-A6-M1-AB1-A7-B4-B5-AB2-B6-A8-A9
                     /
        B0-B1-B2-B3-

But now I've decided I was wrong to merge these projects and would like to split the history of the two projects back into separate respective git repositories.

A0-A1-A2-A3-A4-A5-A6-A7-A8-A9

B0-B1-B2-B3-B4-B5-B6

Both these projects are totally private projects that only I have been working on. So rebasing/recreating all the commits is not an issue. I've been reading a lot of posts on SO and elsewhere on the web but still can't figure out the best was to approach splitting this up.


Solution

  • That's a job for git rebase:

    git clone current_repos new_repos_only_a ; cd new_repos_only_a
    git rebase -i A6
    # skip all B commits, pick all A commits, do not pick the merge commit (should be skipped by git automatically)
    

    (The same for B of course, in a separate new repository).