1

I need to get contents of two repo from say 'modA' and 'modB' into a single directory 'mods' in repo main.

main

-- mods

------ modAfiles

-------modBfiles

There is no straight forward way to do this with git so I am creating a third repo that joins these repos. The contents of these mods repos are guaranteed to not conflict each other. So no need to worry about conflicts.

A sample test case.

    for repo in main modA modB ; do
    git init $repo
    echo "test $repo" > "$repo/test$repo"
    cd $repo
    git add "test$repo"
    git config user.name "$repo test"
    git config user.email "$repo@test.test"
    git commit -m 'Initial commit'
    cd ..
    done
    

Now to set up the fused repo.

    git init fusion
    cd fusion
    git remote add modA ../modA/.git
    git remote add modB ../modB/.git
    git config user.name "fusion test"
    git config user.email "fusion@test.test"
    git fetch --all
    git checkout -b modB_master modB/master
    git checkout -b modA_master modA/master
    git checkout -b merge_master
    git merge modB_master -m "Fuse modA and modB"
    ls

Now the problem is with adding this fused repo to the main repo. If either 'modA' or 'modB' updates I can just delete the 'merge_master' branch of 'fusion' and create it again.

I don't care about the history of the merges. I merely want to join these repos. I also dont need to make changes from 'fusion' or 'main' repo. With subtree I added the 'fusion' repo to 'main' repo. But whenever I change the 'merge_master' it gets force update and also all the commits are shown in the 'main' repo.

How can I add the fusion repo to main without polluting the main repo. Should I be using submodules instead? Also the mods repo get frequently updated. (the creation merge_master will be automated ). Or is there a better way of doing this?

Community
  • 1
  • 1
cswl
  • 443
  • 7
  • 14

2 Answers2

1

You can use submodules for this purpose.

Submodule is a project inside project. The benefits of submodules is that it will allow you to use shared resources for the modules by sharing the same folders and store them under the same root.

Git-Tools-Submodules
git-tip-of-week-git-submodules

enter image description here


Subtree

Another option is using subtree. very similar to submodule but the content is managed in the root project and not as standlone project.

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • If I understood correctly I'm gonna have to create an commit on the main repo everytime the merged repo changes to update the ref in the submodule which super points too.. ( which might happen a lot). So how is it any good from subtree.. – cswl Jan 10 '16 at 10:18
  • submodule - all the content is managed in the folder (sub project inside git) in subtree all the content is managed in the root folder but is stored inside its own folder (single folder) – CodeWizard Jan 10 '16 at 11:02
  • 1
    I understand what they are. I just don't want the content of them connected to my main repo. See my answer. – cswl Jan 10 '16 at 11:51
0

Looks like my issue isn't with merging, subtree or submodules.

It's with uhmm.. git itself.

I don't want to pollute my main repo with unnecessary commits, symlinks or thereof from sub repos.

I want to manage some portions of my stuff which should reside in a single directory yet be on different repositories.

I want their history tracked but not that it matters to my main repo.

And this not how git handles thing.
Even though the repositories may be separate it's still connected.
If I don't want that connection, I'm gonna have to say to git 'Ignore it.'

So I'm just gonna add a .gitignore for the directory, (this cannot be in the directory, as the directory needs to be empty).. And have my fused repo in that directory.

This fused repos can then directly pull the changes from the remotes of the sub repos and merge or even rebase.

But to push the changes I'm gonna have to clone each sub repo outside the main repo.

And what if I ever need to clone the main repo?

Maybe something like android's repo management tool "repo" could be useful for this..

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
cswl
  • 443
  • 7
  • 14
  • I don't want to pollute my main repo with unnecessary commits, symlinks or thereof from sub repos. (this is what submodule are for) --- I want to manage some portions of my stuff which should reside in a single directory yet be on different repositories.(same- submodule) – CodeWizard Jan 10 '16 at 11:53
  • >But to push the changes I'm gonna have to clone each sub repo outside the main repo. [thats right, you will have to pull every submodule] – CodeWizard Jan 10 '16 at 11:54
  • And what if I ever need to clone the main repo? [ You can clone it without any problem, simply clone and then submodule init + update] – CodeWizard Jan 10 '16 at 11:54
  • That is if I add the 'fusion' as a submodule in my repo.. I just want the two repos merged that's all. I don't have the need to track where such merge was made in my main repo (submodule update commit). They're 3 different but related projects of mine, with an unusual directory structure you could say and "repo" just seems like a better tool and approach for my use case. – cswl Jan 10 '16 at 12:37