2

I have been reading an article in which the author instructed: creating a new repository within an existing one, and wondered if it was an error he overlooked. I will verify with him later.

These are the conditions I want to check:

  1. The condition when an existing directory is made into a repository, and files already checked into the main project are also checked in the new (sub) repository. Is this possible?

  2. When a directory tree which contains git repositories is checked in for the first time

  3. When a new empty repository is created under the repository, either by git init, or by copying a .git repository into a new or empty directory?

vfclists
  • 19,193
  • 21
  • 73
  • 92

2 Answers2

2

As mentioned in "Git repository in a git repository", the nested repo is mostly ignored by the parent repo (only a gitlink is recorded)

So any operation on the parent repo won't have an incidence on the nested repo.

If you declared the nested repo as a submodule though, then you can checkout the parent repo and the nested repo, but that submodule will always reference a fixed commit.
If you make any modification in the submodule, you need to commit them, push them and go back to the parent repo, commit and push (to record the new fixed commit of the submodule).
See "True nature of submodules".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Does this mean that commands made within the sub repo would apply to the subrepo? And it that is the case what would happen to a file that had already been committed to the main repo? I'm not planning to,only considering it hypothetically. – vfclists Sep 24 '12 at 22:50
  • @vfclists yes, any command in the subrepo is limited to that nested repo. A file already committed on the main repo would not be within the subrepo: we would be talking about two different file committed in two different repo at two different parent directory (even though their content might be identical). – VonC Sep 25 '12 at 05:28
1

If you want to do this, you should use submodules if you want to have nested repositories. The top level repository will track the latest commit of all the sub repositories and inside them, things can be as you wish. Without this, git will behave in undocumented and unsupported ways so you should probably not try it.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169