2

I have the following directory structure:

Parent/ contains Child1/ Child2/ ... ChildN/

Each of the Child directories is its own git repository with commit histories, etc. I have realized that it would make more sense to make Parent a git repository with the children as subrepositories (presumably using git-subtree or something). This is because the child directories are all components of the same project.

I have looked at various answers, but they all involve pushing/pulling from some remote repo, whereas everything is local to my machine in this case (and I would like to keep it that way--plus, I don't know how to work with remotes).

So my question is: how to I create the Parent repository in such a way that the children are part of it but retain their individual histories and without reference to any remote repository?


Reason this is different from the proposed duplicate: The link about remote repos being on a local hard drive is helpful and definitely makes the other answers on SO more accessible. I just tried the procedure in the linked question on merging two repositories, and it almost does the job but is laborious and labor-prone in my case. In that question, two repositories are to be merged to create a third repository, whereas in my case, a number of repositories need to become the directories of a new repository.

To use the proposed duplicate answer, I would have to make a copy of Parent, say Parent_copy, then delete everything from Parent, create the Parent .git directory, then merge in the Child directories one at a time. In the process, the contents of each Child would be either copied from Parent_copy/Child into Parent or ignored (based on a .gitignore file). Then I could recreate e.g. Parent/Child1 as an empty directory, move the files from Parent, and copy the ignored files from Parent_copy/Child1. So it would work, but is a bit laborious and error-prone. I wonder is there is a more efficient way, ideally treating the Child directories in place?

Zach Boyd
  • 419
  • 1
  • 5
  • 23
  • 2
    A [remote repo can be on the local hard drive](https://stackoverflow.com/questions/10603671/how-to-add-a-local-repo-and-treat-it-as-a-remote-repo). See [this SO](https://stackoverflow.com/questions/13040958/merge-two-git-repositories-without-breaking-file-history) to combine repos (which rewrites history). – Jeff Nov 16 '17 at 21:04
  • 1
    Possible duplicate of [Merge two Git repositories without breaking file history](https://stackoverflow.com/questions/13040958/merge-two-git-repositories-without-breaking-file-history) – JDB Nov 16 '17 at 21:13

1 Answers1

2

Use git submodules and as a submodule url use local repo path.

E.g. to add submodule to parent repository use git submodule add /path/to/some/submodule1.git

Please remember that your existing sub repository should be outside parent repository. Apropriate reference will be copied to parent repo when you call git submodule add ...

running.t
  • 5,329
  • 3
  • 32
  • 50
  • This is pretty close to what I want. Do you know if there is a way to bring along the files that are in the Child directory's .gitignore files without manually looking for all of them? – Zach Boyd Nov 16 '17 at 22:40
  • Do you want to copy them from child subrepo to parent repository during `git submodule add`? Actually you can add files that are in .gitignore to git index and even commit them. If you do so, they will be synchronized next time you synchronize submodule. Still all modifications in that files wil be ignored in both origin child repo and a copy you made with `submodule add` – running.t Nov 16 '17 at 22:49