Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you combine two git repos?

Tags:

I have a main repo (repo 1) that I work with. I have another repo (repo 2) that needs to fit into the first one and I'm not sure how I could have them both in the same folder. The idea is that I have a standard codebase that I need in each project - yet each project is it's own git repo.

/project
    /.git(repo 2)
    /.git(repo 1)
    /repo_2_sub
        /repo_2_sub_sub
            /repo_1_sub_sub
    /repo_1_sub
        /repo_1_sub_sub
        /repo_2_sub_sub

None of the files overlap - but some of the folder structures do. So sometimes certain folders from one repo will be in the other repo.

How can I work so these two repositories build the complete codebase?

UPDATE

Both git repos exist in the same root project folder level. They cannot be submodules since they cross back and forth through each other as shown above. They are not separate folders.

UPDATE 2

Wait, maybe this is easier than I thought. Can you clone a standard codebase repo and then create a new branch that is your project and then just keep merging that branch with the codebase repo each time it changes?

like image 232
Xeoncross Avatar asked Feb 12 '10 19:02

Xeoncross


People also ask

How do I merge two Git repository branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

Can we merge two GitHub accounts?

If you have separate accounts for work and personal use, you can merge the accounts. Tip: We recommend using only one personal account to manage both personal and professional repositories. Warning: Organization and repository access permissions aren't transferable between accounts.


2 Answers

Edit: In order to have two .git directories, when you init them, there would be some extra work. When you init, do:

git --git-dir=.git1 --work-tree=. init
git --git-dir=.git2 --work-tree=. init

Now you have two git dirs in the same directory. Not very pretty. At this point, make sure that you have .git1 and .git2 in both .gitignores, since they aren't ignored by default. Additionally, every git command you run must also have the --git-dir argument to specify which repository you're interacting with. You could setup aliases for that.

Since they're in the same base directory and submodules won't work, you could put '.gitignore' files inside both '.git/info/exclude' directories. Putting them in .git/info/exclude makes sure that the other repo doesn't pick up the exclusions.

It may take some maintenance if the two repos differ by a finer granularity than folders, but if you specify in each .gitignore the files that belong only to the OTHER repo, it should work.

See: gitignore

like image 159
bobDevil Avatar answered Oct 03 '22 19:10

bobDevil


Use git submodule

Update:

The standard code base that needs to be a part of all the repos has to be a seperate distinct git repository.

git submodule add <repo_nick_name> <repo_path>

will add that repo to this one. On cloning and pushing, only the .gitmodules file and the hash at the repo will be pushed/cloned.

git submodule init

and

git submodule update

respectively initializes and updates the remote submodule repos.

For detailed info, check the git community book documentation on git submodules and/or scot chacon's screencast on the same.

like image 44
lprsd Avatar answered Oct 03 '22 19:10

lprsd