232

Can I nest Git repositories? I have:

 /project_root/
 /project_root/my_project
 /project_root/third_party_git_repository_used_by_my_project

Does it make sense to git init/add the /project_root to ease management of everything locally or do I have to manage my_project and the 3rd party one separately?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Jeremy Raymond
  • 5,817
  • 3
  • 31
  • 33

7 Answers7

202

You may be looking for the Git feature called submodules. This feature helps you manage dependent repositories that are nested inside your main repository.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 4
    The chrisjean.com blog does not seem to be current based on just having tried to follow it. The wiki post from Greg may be a bit more complicated, but as a git newbie I prefer accurate to simple... – sage Jul 21 '11 at 16:12
  • The blog seems to work fine now, and unfortunately for sage, the 34 (now 35) up-votes on the comment seems to agree that there is a value in the blog article. Turns out you don't have to sacrifice accuracy for clarity and operation-specific advice. After reading it, I would imagine the author had already researched a bit and probably read the actual [Git documentation](https://git-scm.com/book/en/v2/Git-Tools-Submodules), not just the kernel.org wiki page. The blog author's Git-oriented explanation in a fully contextualized example seems to be quite helpful for a large number of individuals... – Matthew Weber Mar 09 '19 at 14:55
  • 19
    BTW that chrisjean link mentioned above is dead. The updated link is https://chrisjean.com/git-submodules-adding-using-removing-and-updating/ – sprksh Dec 27 '19 at 19:57
36

Place your third party libraries in a separate repository and use submodules to associate them with the main project. Here is a walk-through: Git Tools - Submodules (Pro Git book, 2nd.)

In deciding how to segment a repo I would usually decide based on how often I would modify them. If it is a third-party library and only changes you are making to it is upgrading to a newer version then you should definitely separate it from the main project.

Tom Nguyen
  • 425
  • 3
  • 7
Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
33

Just for completeness:

There is another solution, I would recommend: subtree merging.

In contrast to submodules, it's easier to maintain. You would create each repository the normal way. While in your main repository, you want to merge the master (or any other branch) of another repository in a directory of your main directory.

$ git remote add -f ThirdPartyGitRepo /project_root/
$ git merge -s ours --no-commit ThirdPartyGitRepo/master
$ git read-tree --prefix=third_party_git_repository_used_by_my_project/ -u ThirdPartyGitRepo/master
$ git commit -m "Merge ThirdPartyGitRepo project as our subdirectory"`

Then, in order to pull the other repository into your directory (to update it), use the subtree merge strategy:

$ git pull -s subtree ThirdPartyGitRepo master

I'm using this method for years now, it works :-)

More about this way including comparing it with sub modules may be found in this git howto doc.

Phil
  • 3,282
  • 1
  • 20
  • 16
  • 1
    The subtree merging reference into the git book no longer works. Currently, this appears to be the link: https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_subtree_merge – ericx Nov 07 '16 at 17:25
27

You could add

/project_root/third_party_git_repository_used_by_my_project

to

/project_root/.gitignore

that should prevent the nested repo to be included in the parent repo, and you can work with them independently.

But: If a user runs git clean -dfx in the parent repo, it will remove the ignored nested repo. Another way is to symlink the folder and ignore the symlink. If you then run git clean, the symlink is removed, but the 'nested' repo will remain intact as it really resides elsewhere.

crizCraig
  • 8,487
  • 6
  • 54
  • 53
mikkelbreum
  • 3,021
  • 9
  • 38
  • 41
13

git-subtree will help you work with multiple projects in a single tree and keep separable history for them.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 3
    This feature has been subsequently merged into Git. Here's a nice description: https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree – Brent Bradburn Nov 04 '15 at 23:27
6

Summary.

Can I nest git repositories?

Yes. However, by default git does not track the .git folder of the nested repository. Git has features designed to manage nested repositories (read on).

Does it make sense to git init/add the /project_root to ease management of everything locally or do I have to manage my_project and the 3rd party one separately?

It probably doesn't make sense as git has features to manage nested repositories. Git's built in features to manage nested repositories are submodule and subtree.

Here is a SO question that covers the pros and cons of using each.

lachy
  • 1,833
  • 3
  • 18
  • 27
1

I would use one repository per project. That way, the history becomes easier to browse through.

I would also check the version of the third party library I'm using, into the repository of the project using it.

gnud
  • 77,584
  • 5
  • 64
  • 78