简体   繁体   English

如何将 Git 子模块添加到子目录?

[英]How do I add Git submodule to a sub-directory?

I have a Git repo in ~/.janus/ with a bunch of submodules in it.我在~/.janus/中有一个 Git 仓库,里面有一堆子模块。 I want to add a submodule in ~/.janus/snipmate-snippets/snippets/ , but when I run git submodule add <git@github.com:...> in the snipmate-snippets directory, I get the following error message:我想在~/.janus/snipmate-snippets/snippets/中添加一个子模块,但是当我在snipmate-snippets目录中运行git submodule add <git@github.com:...>时,我收到以下错误消息:

You need to run this command from the top level of the working tree.

So the question is: How do I add a submodule to the snipmate-snippets directory?所以问题是:如何将子模块添加到snipmate-snippets目录?

You go into ~/.janus and run:你进入~/.janus并运行:

git submodule add <git@github ...> snipmate-snippets/snippets/

If you need more information about submodules (or git in general) ProGit is pretty useful.如果您需要有关子模块(或一般的 git)的更多信息, ProGit非常有用。

Note that starting git1.8.4 (July 2013), you wouldn't have to go back to the root directory anymore.请注意,从git1.8.4 (2013 年 7 月)开始,您不必再返回根目录。

 cd ~/.janus/snipmate-snippets
 git submodule add <git@github ...> snippets

( Bouke Versteegh comments that you don't have to use /. , as in snippets/. : snippets is enough) Bouke Versteegh 评论说你不必使用/. ,就像在snippets/.中一样: snippets就足够了)

See commit 091a6eb0feed820a43663ca63dc2bc0bb247bbae :请参阅提交 091a6eb0feed820a43663ca63dc2bc0bb247bbae

submodule: drop the top-level requirement子模块:删除顶层需求

Use the new rev-parse --prefix option to process all paths given to the submodule command, dropping the requirement that it be run from the top-level of the repository.使用新的rev-parse --prefix选项来处理给定子模块命令的所有路径,放弃从存储库的顶层运行它的要求。

Since the interpretation of a relative submodule URL depends on whether or not " remote.origin.url " is configured, explicitly block relative URLs in " git submodule add " when not at the top level of the working tree.由于相对子模块 URL 的解释取决于是否配置了“ remote.origin.url ”,因此当不在工作树的顶层时,显式阻止“ git submodule add ”中的相对 URL。

Signed-off-by: John Keeping签字人:John Keeping

Depends on commit 12b9d32790b40bf3ea49134095619700191abf1f取决于提交 12b9d32790b40bf3ea49134095619700191abf1f

This makes ' git rev-parse ' behave as if it were invoked from the specified subdirectory of a repository, with the difference that any file paths which it prints are prefixed with the full path from the top of the working tree .这使得 ' git rev-parse ' 的行为就好像它是从存储库的指定子目录调用的一样,不同之处在于它打印的任何文件路径都以工作树顶部的完整路径为前缀

This is useful for shell scripts where we may want to cd to the top of the working tree but need to handle relative paths given by the user on the command line.这对于我们可能想要cd到工作树顶部但需要处理用户在命令行上给出的相对路径的 shell 脚本很有用。

I had a similar issue, but had painted myself into a corner with GUI tools.我有一个类似的问题,但是用 GUI 工具把自己画到了一个角落。

I had a subproject with a few files in it that I had so far just copied around instead of checking into their own git repo.我有一个包含一些文件的子项目,到目前为止我只是复制了一些文件,而不是检查到他们自己的 git 存储库中。 I created a repo in the subfolder, was able to commit, push, etc just fine.我在子文件夹中创建了一个 repo,能够提交、推送等就好了。 But in the parent repo the subfolder wasn't treated as a submodule, and its files were still being tracked by the parent repo - no good.但是在父 repo 中,子文件夹没有被视为子模块,并且它的文件仍然被父 repo 跟踪 - 不好。

To get out of this mess I had to tell Git to stop tracking the subfolder (without deleting the files):为了摆脱这种混乱,我不得不告诉 Git 停止跟踪子文件夹(不删除文件):

proj> git rm -r --cached ./ui/jslib

Then I had to tell it there was a submodule there (which you can't do if anything there is currently being tracked by git):然后我不得不告诉它那里有一个子模块(如果 git 当前正在跟踪任何东西,你就不能这样做):

proj> git submodule add ./ui/jslib

Update更新

The ideal way to handle this involves a couple more steps.处理此问题的理想方法涉及更多步骤。 Ideally, the existing repo is moved out to its own directory, free of any parent git modules, committed and pushed, and then added as a submodule like:理想情况下,现有的 repo 被移出到它自己的目录,没有任何父 git 模块,提交和推送,然后作为子模块添加,如:

proj> git submodule add git@bitbucket.org:user/jslib.git ui/jslib

That will clone the git repo in as a submodule - which involves the standard cloning steps, but also several other more obscure config steps that git takes on your behalf to get that submodule to work.这会将 git repo 克隆为子模块 - 这涉及标准克隆步骤,但也包括 git 代表您执行的其他几个更晦涩的配置步骤,以使该子模块工作。 The most important difference is that it places a simple .git file there, instead of a .git directory, which contains a path reference to where the real git dir lives - generally at parent project root .git/modules/jslib.最重要的区别是它在那里放置了一个简单的 .git 文件,而不是 .git 目录,该目录包含对真实 git 目录所在位置的路径引用 - 通常在父项目根目录 .git/modules/jslib 中。

If you don't do things this way they'll work fine for you, but as soon as you commit and push the parent, and another dev goes to pull that parent, you just made their life a lot harder.如果你不以这种方式做事,它们会为你工作得很好,但是一旦你提交并推动父母,而另一个开发者去拉那个父母,你只会让他们的生活变得更加艰难。 It will be very difficult for them to replicate the structure you have on your machine so long as you have a full .git dir in a subfolder of a dir that contains its own .git dir.只要您在包含自己的 .git 目录的目录的子文件夹中有完整的 .git 目录,他们就很难复制您在机器上的结构。

So, move, push, git add submodule, is the cleanest option.所以,move、push、git add submodule,是最干净的选择。

For those of you who share my weird fondness of manually editing config files, adding (or modifying) the following would also do the trick.对于那些和我一样喜欢手动编辑配置文件的人来说,添加(或修改)以下内容也可以解决问题。

.git/config (personal config) .git/config (个人配置)

[submodule "cookbooks/apt"]
    url = https://github.com/opscode-cookbooks/apt

.gitmodules (committed shared config) .gitmodules (提交的共享配置)

[submodule "cookbooks/apt"]
    path = cookbooks/apt
    url = https://github.com/opscode-cookbooks/apt

See this as well - difference between .gitmodules and specifying submodules in .git/config?也看到这一点 - .gitmodules 和在 .git/config 中指定子模块之间的区别?

one-liner bash script to help facility Chris's answer above, as I had painted myself in a corner as well using Vundle updates to my .vim scripts.单行 bash 脚本来帮助 Chris 在上面的回答,因为我把自己画在一个角落里,以及使用 Vundle 更新我的 .vim 脚本。 DEST is the path to the directory containing your submodules. DEST是包含您的子模块的目录的路径。 Do this after doing git rm -r $DEST在执行git rm -r $DEST之后执行此操作

DEST='path'; for file in `ls ${DEST}`; do git submodule add `grep url ${DEST}/${file}/.git/config|awk -F= '{print $2}'` ${DEST}/${file}; done

cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM