Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Git repositories?

Tags:

git

People also ask

Can I have nested Git repositories?

Git allows you to include other Git repositories called submodules into a repository. This allows you to track changes in several repositories via a central one. Submodules are Git repositories nested inside a parent Git repository at a specific path in the parent repository's working directory.

What is a submodule in Git?

A git submodule is a record within a host git repository that points to a specific commit in another external repository. Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

Can I merge 2 repos?

You have two repositories on git, say repo1 and repo 2, each with a readme.md file. Then two merge these two, you could follow these steps: Step 1: Clone one of the repositories (say repo1). Step 2: Create another remote in this clone which points to repo2-our second repository.


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


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.


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.


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.


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


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.


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.