Arthur
Arthur

Reputation: 3473

Git - Link other repository to my repository

I have some repo A and repo B. Project in repo A depends on project from repo B. I want to do such that when some user try to pull sources from my repo A, on pull from repo A, sources from repo B also must be pulled in some folder. I have see, what SVN can link some SVN repo to other SVN repo. How can I link git repos?

Thanks.

Upvotes: 1

Views: 303

Answers (1)

Messa
Messa

Reputation: 25201

Git has submodules for this.

Example:

$ git init repo_a
Initialized empty Git repository in /Users/messa/temp/submodule-example/repo_a/.git/
$ git init repo_b
Initialized empty Git repository in /Users/messa/temp/submodule-example/repo_b/.git/

$ cd repo_b
$ echo 'This is Project B.' >> README.txt
$ git add README.txt 
$ git commit -am 'Initial commit in B'
[master (root-commit) 5158772] Initial commit in B
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

$ cd ../repo_a
$ echo 'This is Project A, that depends on Project B.' >> README.txt
$ git add README.txt 
$ git commit -am 'Initial commit in A'
[master (root-commit) 7e8275b] Initial commit in A
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

$ git submodule add ../repo_b project_b
Cloning into 'project_b'...
done.
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   .gitmodules
#   new file:   project_b
#
$ git commit -am 'Added submodule project_b'
[master c8fc469] Added submodule project_b
 2 files changed, 4 insertions(+)
 create mode 100644 .gitmodules
 create mode 160000 project_b

$ tree
.
├── README.txt
└── project_b
    └── README.txt

1 directory, 2 files

You can exactly control what commit from repo_b will be linked from repo_a.

For more information, see git help submodule or the link above.

But there is one problem - when somebody clones repo_a, the project_b will be there, but empty. After git clone, git submodule init and git submodule update will be needed.

I don't have real-world experience with submodules, but I've seen some criticism - for example this article. I think it is important to choose the right workflow to avoid most of the problems.

Upvotes: 3

Related Questions