Christoph Schiessl
Christoph Schiessl

Reputation: 6868

How to combine two branches from two different repositories in a single repository?

The structures of my Git repositories look like this:

A-B-C-D-E-F   # master branch in separate repo1
A-B-C-D-E-G-H # master branch in separate repo2

A-H are simple commits. As you can see the repositories are related (repo2 is a fork of repo1). I'm trying to combine these two repositories in one.

Afterwards the single repository should have the following structure:

A-B-C-D-E-F   # master branch of previous repo1
        \
         \
          G-H # master branch of previous repo2

I've already spent a lot of time reading the Git User's Guide and so on. However, this (special) case of use doesn't seem to be documented anywhere.

Upvotes: 48

Views: 26380

Answers (3)

Peter Burns
Peter Burns

Reputation: 45361

You can treat another git repository on the same filesystem as a remote repo.

In the first, do the following:

git remote add <name> /path/to/other/repo/.git
git fetch <name>
git branch <name> <name>/master #optional

Now they're both branches in a single repository. You can switch between them with git checkout, merge with git merge, etc.

Upvotes: 72

Pistos
Pistos

Reputation: 23812

I think Jim is right. Also bear in mind that if two commits do exactly the same patch/diff against the code, they will have exactly the same SHA1 hash. So A through E should have the same SHA1 in both repos, so it shouldn't be a problem to merge from one to the other, and keep the merged repo as the sole repo to move forward with.

You can setup on repo1 a tracking branch for repo2, and then keep them distinct that way, and delay the merge to whenever you want.

Upvotes: 4

Jim Puls
Jim Puls

Reputation: 81142

Your picture suggests that you don't really want to "combine the two repositories" so much as merge commits G and H in to repo1. You should be able to do something as simple as add repo2 as a remote to repo1 and fetch/pull the changes in.

Upvotes: 7

Related Questions