git - 如何 `git submodule add` 现有子存储库?

标签 git git-submodules cloud9-ide

问题

如何在 git 中添加现有的子仓库作为子模块?

原因

我有一个私有(private) codespace子模块随机分布的 super 模块:

codespace (git repo, private)
├── Archived_projects (git repos)
└── Projects
    ├── project-foo (git repo)
    └── project-bar (git repo)

有时子模块的提交还没有准备好被推送。但我希望在推送 super 模块时保存它们 codespace .
codespace是克隆到 c9.io 的 repo 协议(protocol)工作区或其他地方。

我做什么

linus@machine /cygdrive/f/__Storage__/Workspace
$ git clone https://github.com/octocat/Spoon-Knife.git
Cloning into 'Spoon-Knife'...
$ cd Spoon-Knife/
$ git clone https://github.com/octocat/Spoon-Knife.git ./foo/bar
Cloning into './foo/bar'...
$ git add .

来自cmd.exe

> git submodule add https://github.com/octocat/Spoon-Knife.git ./foo/bar
'foo/bar' already exists in the index
> cat .gitmodules
cat: .gitmodules: No such file or directory

来自 cygwin.exe (bash)

$ git submodule add https://github.com/octocat/Spoon-Knife.git ./foo/bar
': not a valid identifier/Git/mingw64/bin/gettext.sh: line 89: export: `sm_path
'' already exists in the index
$ cat .gitmodules
cat: .gitmodules: No such file or directory

引用

git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>]
              [--reference <repository>] [--depth <depth>] [--] <repository> [<path>]

<repository> is the URL of the new submodule’s origin repository.

<path> is the relative location for the cloned submodule to exist in the superproject. If <path> does not exist, then the
submodule is created by cloning from the named URL. If <path> does exist and is already a valid Git repository, then this is
added to the changeset without cloning. This second form is provided to ease creating a new submodule from scratch, and
presumes the user will later push the submodule to the given URL.

In either case, the given URL is recorded into .gitmodules for use by subsequent users cloning the superproject. If the URL
is given relative to the superproject’s repository, the presumption is the superproject and submodule repositories will be
kept together in the same relative location, and only the superproject’s URL needs to be provided: git-submodule will
correctly locate the submodule using the relative URL in .gitmodules.

如果<path>确实存在并且已经是一个有效的 Git 存储库,然后将其添加到变更集中而无需克隆。

为什么这对我不起作用?

最佳答案

出了什么问题

你做错了

$ git add .

这会将所有内容以及 foo/bar 添加到当前存储库的索引中(准备好提交)。

正确的方法

如果你不这样做并继续

$ git submodule add https://github.com/CarloWood/XYZ.git foo/bar

那应该可以了;这会检测到 foo/bar 是一个已经被克隆的 存储库并将其作为子模块添加到当前存储库。

请注意,不需要先克隆。你明确地说你已经这样做了,但是为了让其他读者清楚起见,我想指出,如果你在 git add . 之前也省略了克隆(所以没有 foo/bar at all now) 然后上面的 git submodule add ... 会看到还没有任何东西,然后简单地为你克隆它。

请注意,方法之间存在细微差别。如果您从克隆开始,那么 foo/.git 将是一个目录,而如果您使用 git submodule add 进行克隆,那么这个 .git 存储库放在父项目的 .git/modules/foo 中,foo/.git 是一个包含该路径的文件。但是,没有真正的区别,因为使用 .git 文件指向其他任何地方是通用的,可以在任何地方使用;你不能从 .git 是一个文件或目录来得出任何结论。

关于git - 如何 `git submodule add` 现有子存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32776018/

相关文章:

php - 在 PHP 应用程序 Openshift 上提交更改后出现 404 Not Found 错误

git - 多个 ssh key

git - 如何在 git 日志图中显示相对提交号?

git-submodules - git submodule update 只需要最初?

node.js - 无法在云IDE中运行meteor,需要帮助了解meteor内存使用情况

Git 作为备份和版本控制系统

git - 当 git 子模块引用的 repo 被删除时会发生什么?

git - 避免为每个子模块重新输入密码

ruby-on-rails - Ruby on Rails OmniAuth Facebook 适用于 Heroku 上的 Live App 但不适用于 Cloud9 上的测试应用

selenium - Cloud9 Watir/ Selenium 测试