通过子模块在Git中显示外部代码依赖性

Léo*_* 준영 4 git git-submodules

解决问题:通过子模块显示外部代码依赖性:感谢VonC!

当前问题:在两个文件夹中一个没有重复内容的子模块

当前问题的临时解决方案:将文件夹的名称更改为bin-github,因为作为子模块的文件夹的名称是bin.这意味着我需要在我的家中复制内容.

我的HOME文件夹依赖于〜/ bin.我的文件位于主页的存储库Masi,而位于Github 的repo bin的〜/ bin .我家的相关文件夹结构:

~
|-- [drwxr-xr-x] bin
    | -- fileA
    ` -- folderA
    ...
Run Code Online (Sandbox Code Playgroud)

我想知道如何在Masi上显示make~/bin是Git的一个子模块.

你怎么能〜/ bin中的〜/ bin是我的Git的一个子模块?


#3回复VonC的评论:

我的.gitmodules

[submodule "bin"]   
         path = bin
         url = git://github.com/masi/bin.git
Run Code Online (Sandbox Code Playgroud)

我觉得我不需要再添加子模块了,因为我已经在我的.gitmodules中了.

我跑

Sam master $ git submodule update
Sam master $ git submodule foreach git pull
Sam master $ ls bin 
Run Code Online (Sandbox Code Playgroud)

#2回复VonC的回答:

我将.gitmodules更改为以下内容,以便在我的家中没有它的副本.

[submodule "bin"]
    path = bin
    url = git://github.com/masi/bin.git
Run Code Online (Sandbox Code Playgroud)

这似乎是一个不同的情况,因为我不能像上面那样拉动子模块bin文件夹.

我现在在一个新克隆的git-repo上得到以下内容

Sam master $ git submodule git init
error: pathspec 'git' did not match any file(s) known to git.
error: pathspec 'init' did not match any file(s) known to git.
Did you forget to 'git add'?                          # I am not sure what this means
Sam master $ git submodule foreach git pull
Sam master $ git submodule update
Run Code Online (Sandbox Code Playgroud)

我的.gitmodules中有外部存储库.为什么要问我git add


回复VonC的回答:

我跑

git submodule add git://github.com/masi/bin.git bin-github-copy
Run Code Online (Sandbox Code Playgroud)

因为我无法添加与现有文件夹同名的新仓库bin.

我的.gitmodules是

[submodule "bin-github-copy"]
    path = bin-github-copy
    url = git://github.com/masi/bin.git
Run Code Online (Sandbox Code Playgroud)

我跑

git clone git://github.com/masi/Sam.git
Run Code Online (Sandbox Code Playgroud)

我得到一个emty bin -directory.

我怎样才能获得bin的内容?

Von*_*onC 5

首先,不要忘记使用Git,您不能"添加"目录,只能访问该目录中至少一个文件的内容.

那你有两个选择:

  • 子模块,以便Masi []存储在GitHub 的bin目录中引用文件的特定提交(或者存储在GitHub上的最新分支提交)

  • 子树合并,以便将两个存储库混合在一起,并且更容易直接从Masi存储库跟踪对bin中文件的修改.在您的方案中,这可能更简单.

有关两种策略的更多信息,请参阅此答案.


在子模块路径上,声明子模块只是工作的一半.您仍然必须首次初始化您的子模块

git submodule git init
Run Code Online (Sandbox Code Playgroud)

然后你需要拉你的子模块:

git submodule foreach git pull
Run Code Online (Sandbox Code Playgroud)

然后更新您的子模块

git submodule update
Run Code Online (Sandbox Code Playgroud)

另见Git子模块的实际例子,第一部分Git子模块,第二部分.

.gitmodules文件仅存储路径,但提交版本确实与主项目一起存储.
在GitHub上创建了一个项目来演示会发生什么.具体看一下这个添加子模块的提交,看看发生了什么:

+Subproject commit 60be4b09f51d2560802ebd744893bb6f737ef57c
Run Code Online (Sandbox Code Playgroud)