45

I have a main git repository A and we are using sources out of another git repository B in a subdirectory of our main project. Now it would be good to have the B repository checked out within the A repository in this used subdirectory. If someone else then clones the repository of course he should get our main repository A and within that automatically the B repository.

Let me visualize the directory structure:

+ main_repository       - the root directory of the main Repository
  + src                 - directory containing the source 
    + foreignRepo       - this should be the root directory of another git repo
  + binaries
  + other

This must be also known in the remote repository, just a local copy doesn't help me, because other people check this out and must be able to compile all the stuff.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Buuuh
  • 451
  • 1
  • 4
  • 3
  • A blog post explains the pros ans cons of each approach [Git Submodule: Git Subtree](http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/). – Paul Rougieux Feb 16 '16 at 13:34

3 Answers3

27

You'll want to read about Git submodules.

cborgia
  • 1,399
  • 1
  • 10
  • 10
Adrian Petrescu
  • 16,629
  • 6
  • 56
  • 82
12

Use git subtree in git version 1.7.11 and above. git subtree is superior to git submodule because:

  • Management of a simple workflow is easy. Older versions of git are supported (even before v1.5.2)
  • The sub-project’s code is available right after the clone of the super project is done
  • git subtree does not require users of your repository to learn anything new, they can ignore the fact that you are using subtree to manage dependencies
  • git subtree does not add new metadata files like git submodule does (such as .gitmodule)
  • Contents of the subtree can be modified without having a separate repository copy of the dependency somewhere else

Additionally, if you need to detach a subtree of your existing repository into a new repository, git subtree split can help you with that.

UPDATE February 2020: Nowadays I like subrepo even more.

John McGehee
  • 9,117
  • 9
  • 42
  • 50
11

You can nest two git repo's without using submodules. Suppose ChildRepo is a subdirectory of ParentRepo, and both are git repositories.

+ ParentRepo
  - ChildRepo

If you add a file to ChildRepo, ParentRepo will ignore it. When you commit it it will be added to ChildRepo. It is not possible to add files within ChildRepo to ParentRepo.

More info: Nested GIT repo gotchas!

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • 1
    I just tried nesting a child repo within a parent repo. when I added a file to child repo, I did `cd ../` and `git status` and the parent repo did *not* ignore the new file. – chharvey May 04 '14 at 17:51
  • 2
    Do you need put `ChildRepo` in `.gitignore`? – somenxavier Aug 23 '21 at 15:59
  • I forgot to push the few days work of ChildRepo to github. My ParentRepo contents were destroyed somehow. I restored it. But the ChildRepo is empty (I thought ParentRepo will at least keep the files of ChildRepo files, but the folder is totally empty). It is few days work. Would be glad to get it back. Is there any way? – Ahmad Ismail Aug 06 '23 at 20:40