Git subtrees
Git submodules is a common way, gaining much ground since introduced, in order to deal with the situation where one may want to add a project (repo) within another project (repo), as the other answers have correctly described.
Nevertheless, one could argue that the submodules way is not the only way and occassionaly not the proper way to go, depending on established workflows, for several reasons which I am not going to analyze and which are briefly mentioned in several pages such as this and this. The most important is arguably this:
When Git drops into conflict resolution mode, it still doesn’t update the submodule pointers – which means that when you commit the merge after resolving conflicts, you run into the same problem ...: if you forgot to run git submodule update, you’ve just reverted any submodule commits the branch you merged in might have made.
Of course in a perfect working flow this would never happen.
Another important reason is that the popular PyCharm IDE (when this is written; there is a very old issue for that) and possibly others as well do not fully implement git submodules and the coder will lose among others the nifty funtionality of the IDE displaying all changed lines in the submodule.
Therefore an alternative way to deal with this issue is to use subtrees. Notice that subtrees and subtree merging is not exactly the same thing, but this is again another matter. The excellent Progit book in its 2nd edition briefly covers the latter, but not a single reference for the former.
So in a practical example, in order to deal with the situation under concern, let assume a subproject
to be consumed in a project
:
$ git remote add subproject_remote (url)
# subproject_remote is the new branch name and (url) where to get it from, it could be a path to a local git repo
$ git subtree add —-prefix=subproject/ subproject_remote master
# the prefix is the name of the directory to place the subproject
$ git commit -am "Added subproject"
# possibly commit this along with any changes
If the subproject changes, to pull them into project
:
git subtree pull —prefix=subproject subproject_remote master
...or the opposite (if changes are made in the subproject
inside the project
):
git subtree push —prefix=subproject subproject_remote new_branch
An analytical but rather clattered tutorial in this link.
This functionality has got some drawbacks as well, for instance a lot of people find the working flow cumbersome and more complicated, but again this depends on the particular established workflows.