1

我有一些repo Arepo B。中的项目repo A取决于来自 的项目repo B。我想这样做,当某些用户尝试从 my 中提取源时repo A,在 pull fromrepo A时,来自的源repo B也必须被拉到某个文件夹中。我已经看到,什么 SVN 可以将一些 SVN 存储库链接到其他 SVN 存储库。如何链接 git repos?

谢谢。

4

1 回答 1

3

Git为此提供了子模块

例子:

$ 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

您可以精确控制来自 repo_b 的哪些提交将从 repo_a 链接。

有关更多信息,请参阅git help submodule或上面的链接。

但是有一个问题——当有人克隆 repo_a 时,project_b 会在那里,但是是空的。之后git clonegit submodule initgit submodule update需要。

我没有子模块的实际经验,但我看到了一些批评——例如这篇文章。我认为选择正确的工作流程以避免大多数问题很重要。

于 2012-10-26T13:08:20.740 回答