Create a git repository in some directory. Add all the files in that directory and commit them. Then create another git repository in a parent directory and add all files. Clone that repository elsewhere. Note that the files in the sub-repository are missing in the cloned repository. How can I get Git to add all files in the nested repository? Note that I don't want submodules - I want the files from the nested repository to be added as well.
-
2What exactly are you trying to do? Tracking one repo inside of another is a really bad idea. This is what submodules are for. – Lily Ballard Mar 12 '12 at 20:49
-
I'm trying to do exactly what I said I'm trying to do - I'm trying to add all files from a directory recursively and don't want to trip over nested repositories. – dromodel Mar 12 '12 at 20:57
-
Are you trying to track the files in the subdirectory in two separate git repos at once? That's a bad idea. Are you trying to track the actual nested git repo in the parent repo? That's an even worse idea. Instead of describing the actions you're trying to take, why not describe what goal you're trying to accomplish? – Lily Ballard Mar 12 '12 at 21:14
-
Related: https://stackoverflow.com/questions/33406739/git-treat-nested-git-repos-as-regular-file-folders and https://stackoverflow.com/questions/47008290/how-to-make-outer-repository-and-embedded-repository-work-as-common-standalone-r – Gabriel Staples Jun 14 '20 at 04:28
2 Answers
Chances are that you do want something very much like submodules (but perhaps not literal submodules). For instance, it seems to be fairly common to have a large project that consists of some number N of sub-projects (N > 1), where, for good source and/or organizational reasons, each sub-project should have its own git repo.
Submodules allow you to have one big "super-container" that simply calls out all the submodules:
super
sub1
sub2
sub3
Here each submodule is independent (as far as it knows / cares, anyway) and "super" just collects the three.
The catch is that during development, you often (but not always) want, e.g., "whatever is latest on branch devel
" from every subproject. Submodules, however, track specific commits—commit e01ab7c of sub1, 58125f7 of sub2, etc., which may not be "the latest". You can of course go into each sub-project and "git co devel" to move the branch, then to go to the super-project and fix up each submodule to point to that, but it sure would be convenient if you could tell git "just get me the tip of branch devel
of every submodule".
But git won't do that. We use a massive (and somewhat flaky) script to achieve something along these lines instead. (It also collects commits that span multiple submodules, since sometimes a change affects, e.g., both sub1
and sub3
in the above kind of example.)

- 448,244
- 59
- 642
- 775
You haven't clarified your question yet, so I'll give you a few answers.
If you don't actually care about this subdirectory as a separate repository anymore, and just want to fold it into the parent repository, i.e. you don't want a nested repository at all, just copy the contents of the repository in. You could also simply remove the .git directory from what you already cloned in.
If you do want a nested repository, handled appropriately, then you want a submodule - that's a repo within a repo, managed with the git submodule
command among other things. Have a look at the manpage or for documentation online.
If you want to have it both ways, you could use git-subtree
, which lets you merge a separate repo into a subtree, and split it apart to merge it back into the other repo. It's not without its complications, so be sure to familiarize yourself with its documentation before you decide to use it.

- 479,068
- 72
- 370
- 318
-
I want Git to treat a nested .git directory like it's data in the parent repository. Just pretend for a moment that what I have is a single Git repository that's confused by some files that look Git-like nested within it. – dromodel Mar 12 '12 at 23:33
-
In summary: no, you don't want that. I promise. Look into submodules further. – Cascabel Mar 13 '12 at 00:28
-
It's just bits to me in this case. I don't really know what's in it and I don't care. I guess I could tar it up so Git doesn't see that there may be .git directories inside but I was hoping that a simpler solution existed, like --ignore-nested or something like that. – dromodel Mar 13 '12 at 20:42
-
I promise you, you are setting yourself up for all kinds of pain if you try to track the files in two repositories at once, without the repositories knowing about each other. You can probably ignore the .git directory if you want, but please, listen to us. We're not crazy. – Cascabel Mar 16 '12 at 03:40
-
FYI: renaming the nested `.git` folders to anything else, such as `..git`, also causes git to just treat them as normal folders and copy them in to the outer repo. This is a crude work-around, but it does indeed work. – Gabriel Staples Jun 14 '20 at 04:27