在其他几个项目中使用较小的项目作为 git 子模块 - 无法使其工作

using a smaller project as a git submodule in several other projects - can't make it work

我在这里不止一次看到这个问题以一种或另一种形式出现,这就是让我选择这条路线的原因。所以我有一个小型项目(网络内容),它有自己的 gruntfile 和 git 存储库,并且正在积极开发中。我想在其他几个项目中使用该开发的 result,也就是说,我不想要或不需要 gruntfile 或 sass 文件等。但是,如果这个共享项目中有一个更新,我希望能够将这些更新提取并合并到当前项目中的文件中。我还想对这些文件进行修改,使其适应当前项目的需要。

我的理解是,我应该在我想要这些文件的任何其他项目中将其设为 git 子模块。我必须说我对 git 还很陌生,到目前为止我已经将它用于一些个人项目 - 并且喜欢它 - 但没有协作(所以还没有拉动和合并),我觉得我还不是很了解

无论如何,我将共享项目(我们称之为 SUB)添加为我当前正在处理的项目(我们称之为 A)中的子模块。因此 SUB 现在作为文件夹存在于 A 中,git status 现在将此文件夹列为 modified。执行 git add SUB/ 不会改变这一点。做 git merge SUB/ 只会让我获得 merge: SUB/ - not something we can merge。 ??如何将更新的文件从 SUB 获取到 A?还是我以错误的方式解决这个问题?我还在某处找到了SO上的子树合并策略,也许那个更合适?

我正在使用 git v2.1.4,afaik 在 v 2.10.1 中对 git 子模块进行了更改 - 不确定这是否是一个问题。

所以首先,ASUB是两个不同的repo,有自己的生命。

A 的角度来看,SUB 只是对 SUB 中特定提交的 reference/link。
因此,当您在 SUB 中更改某些内容时,您需要提交这些更改:

$ cd SUB/
$ git commit -a -m "Improve SUB framework loading times"
[master f78dc91] Improve SUB framework loading times"
 10 files changed, 228 insertions(+), 28 deletions(-)
$ git push

现在您已经向 SUB 添加了一个新提交 (f78dc91),您可以返回 A 并输入:

$ cd ..
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   SUB (new commits)

您需要 git add 这些 SUB 更改(即引用新提交 f78dc91):

$ git add SUB
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   SUB

然后提交:

$ git commit -m "A now uses a new version of SUB improving loading times."
$ git push

因此,如您所见,在 SUB 中提交和推送您的更改,以及在 A 中集成这些更改是两个独立的操作,如果您希望 A得到改变。

其他评论

git merge SUB/

你只能合并引用,这意味着提交、分支和标签(显然只有 A 仓库的引用)所以这个命令没有任何意义。 (不能合并文件或目录)