Join two git repos in a GNU stow like manner

Viewed 105

I have two git repositories with a few shared subdirectories between them, as shown below.

Repo 1                      Repo 2
|-Folder A                  |-Folder A
|  |-docA.c                 |  |-docX.c
|-Folder B                  |-Folder B
|  |-docB.c                 |  |-docY.c
|-Folder C                  |-Folder D
|  |-docC.c                 |  |-docZ.c

Now, I am trying to create a third repository that joins repo 2 into repo 1, in a GNU stow like manner. However, I would like to have the documents imported form repo 2 recognised as belonging to that repository, emulating the behaviour of git submodule. As far as I could figure out, submodule only lets me add an entire subfolder to my project form another repository. Just for reference, the third repository should have the following structure:

Repo 3
|-Folder A
|  |-docA.c
|  |-docX.c
|-Folder B
|  |-docB.c
|  |-docY.c
|-Folder C
|  |-docC.c
|-Folder D
|  |-docZ.c

Is this even possible to achieve? Or am I better off just keeping the two repositories separate, track their changes separately and using GNU stow when needed?

1 Answers

I'm not sure why you would want to do this, but technically you could do the following:

git clone repo1 repo3
cd repo3
git remote rename origin repo1 # less confusing this way
git remote add origin <repo3 url>
git remote add repo2 /path/to/repo2 # or URL
git fetch repo2
git merge --allow-unrelated-histories repo2/master # or whatever branch you use

and then periodically :

git fetch repo1
git fetch repo2
git merge repo1/master
git merge repo2/master

As long as there is no conflict between the files in repo1 and repo2 this should work... However the commit graph (git log --graph) will get messy easily.

Another option would be to rebase instead of merging:

git clone repo1 repo3
cd repo3
git remote rename origin repo1 # less confusing this way
git remote add origin <repo3 url>
git remote add repo2 /path/to/repo2 # or URL
git pull --rebase repo2 master # or whatever branch you use

and then periodically :

git pull --rebase repo1 master
git pull --rebase repo2 master
Related