Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 517
Christoph Schiessl Avatar asked Oct 28 '08 20:10

Christoph Schiessl


People also ask

Can we merge two different repositories?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

How do I merge two GitHub repository branches?

Switch to the main branch using the git checkout command, then merge the branch using the git merge command along with the branch name.

How do I merge two branches?

To do a merge (locally), git checkout the branch you want to merge INTO. Then type git merge <branch> where <branch> is the branch you want to merge FROM. Because the history of master and the history of make_function share common ancestors and don't have any divergence descendents, we get a "fast-forward" merge.


1 Answers

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.

like image 189
Peter Burns Avatar answered Sep 19 '22 05:09

Peter Burns