如何`git submodule add`现有子存储库? [英] How to `git submodule add` Existing sub Repository?

查看:438
本文介绍了如何`git submodule add`现有子存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在git中将现有子存储库添加为子模块?

How to add existing sub repository as a submodule in git?

我有一个私有的 codespace 超级模块,其子模块随机分布:

I have a private codespace supermodule with submodules scattered randomly:

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

有时子模块有提交还没准备好被推。但是我希望在推送超级模块 codespace 时保存它们。

codespace 是一个回购克隆到 c9.io 工作区或其他地方。

Sometimes submodules have commits not ready to be pushed. But I want them to be saved while pushing supermodule codespace.
codespace is a repo cloned to c9.io workspace or other places.

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



参考



Reference

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存储库,然后将其添加到变更集中而不进行克隆。

If <path> does exist and is already a valid Git repository, then this is added to the changeset without cloning.

为什么在我的情况下不起作用?

Why this doesn't work in my case?

推荐答案

哪里出错了?

$ git add .

这会添加所有内容,因此 foo / bar ,指向当前存储库的索引(已准备好提交)。

That adds everything, so also foo/bar, to the index of the current repository (ready to be committed thus).

如果您不这样做,请继续

If you just don't do that and continue with

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

然后应该起作用;这将检测到foo / bar是已经克隆的
存储库,并将其作为子模块添加到当前存储库中。

then that should work; this would detect that foo/bar is an already cloned repository and add it to the current repository as a submodule.

请注意,无需克隆第一。您明确地说您已经这样做了,但是为了清楚起见,对于其他读者,我想指出的是,如果您也在 git add之前省略克隆。(所以现在根本没有foo / bar),然后上面的 git子模块add ... 会看到还没有任何东西,然后简单地为您克隆它。

Note that it is not needed to clone first. You explicitly say you already have done that, but for clarity for other readers I'd like to point out that if you omit the clone right before the git add . too (so there isn't a foo/bar at all now) then the above git submodule add ... would see there isn't anything yet and then simply clone it for you.

请注意,方法之间存在微小差异。如果您从克隆开始,那么 foo / .git 将是一个目录,而如果您使用 git子模块add 克隆,然后将此 .git 存储库放入父项目的 .git / modules / foo foo / .git 是包含该路径的文件。但是并没有真正的区别,因为使用 .git 文件指向其他任何地方都是通用的,并且可以在任何地方使用。您无法从 .git 作为文件或目录得出任何结论。

Note that there is a minor difference between methods. If you start with cloning then foo/.git will be a directory, while if you use git submodule add to do the cloning then this .git repository is put in .git/modules/foo of the parent project and foo/.git is a file containing the path to that. There is no real difference however as using a file for the .git to point anywhere else is generic and could be used anywhere; you can not conclude anything from .git being a file or directory.

这篇关于如何`git submodule add`现有子存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆