指定为相对路径时无法添加 git 子模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1974181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't add git submodule when specified as a relative path
提问by deepblue
I'm trying to add a submodule to my git repo, and I'm getting this error in return:
我正在尝试向我的 git repo 添加一个子模块,但我收到此错误作为回报:
remote origin does not have a url defined in .git/config
any ideas about what this might be? I tried googling for it but only one vague link comes up.
关于这可能是什么的任何想法?我尝试在谷歌上搜索它,但只出现了一个模糊的链接。
I'm doing this:
我这样做:
git submodule add ../extern/Lib1 lib
I'm expecting this to create a submodule lib/Lib1
I'm aware that this will only create a reference and that I then have to update/init (not crystal clear on this part, haven't gotten that far; I'm just learning the submodule command).
我期待这会创建一个子模块lib/Lib1
我知道这只会创建一个引用,然后我必须更新/初始化(这部分不是很清楚,还没有那么远;我只是在学习子模块命令)。
采纳答案by VonC
Does ../extern/Lib1
refer to a Git repository?
If it does not, Git wouldn't know how to had the Git repo url to its .gitmodule
Also, try:
是否../extern/Lib1
引用 Git 存储库?
如果没有,Git 将不知道如何将 Git 存储库 url 设置为它的.gitmodule
另外,请尝试:
- with the destination
lib
notalready existing (even empty) - with an absolute path instead of a relative path (you can use a relative one, but just in case, it is worth a try here)
- 目的地
lib
不存在(甚至是空的) - 使用绝对路径而不是相对路径(您可以使用相对路径,但为了以防万一,这里值得一试)
Some good sources on submodules are:
子模块的一些好的来源是:
- chapter 8 of Git User's manual
- Wiki page on Git Submodule Tutorial
and of course - Git submodule man page
Since only the absolute path works here, it means the relative path need a reference to be compared against.
That reference is the "remote origin" which should be in your DirName/NewRepo_withSubmodules/.git/config
file, like so:
由于这里只有绝对路径有效,这意味着相对路径需要一个引用来进行比较。
该引用是应该在您的DirName/NewRepo_withSubmodules/.git/config
文件中的“远程来源” ,如下所示:
$ cat .git/config
...
[remote "origin"]
url = /path/to/DirName/NewRepo_withSubmodules/.git
fetch = +refs/heads/*:refs/remotes/origin/*
...
If you do have that section in ../DirName/NewRepo_withSubmodules/.git/config
file, you should be able to add ../Extern/Lib1
as a submodule using a relative path.
如果您在../DirName/NewRepo_withSubmodules/.git/config
文件中有该部分,则应该能够../Extern/Lib1
使用相对路径将其添加为子模块。
All the above is inspired from the following section of the git submodule man page:
以上所有内容的灵感来自 git 子模块手册页的以下部分:
<repository>
is the URL of the new submodule's origin repository.
This may be either an absolute URL, or (if it begins with./
or../
), the location relative to the superproject'sorigin
repository.
<repository>
是新子模块的原始存储库的 URL。
这可以是绝对 URL,或者(如果以./
或开头../
),相对于超级项目的origin
存储库的位置。
So if NewRepo_withSubmodules
is a local Git repo which has just been created (and has of course no "origin"), an artificial "remote origin" ought to be defined (even if the origin points to itself), if only to allow relative url for other submodule repositories to be used.
因此,如果NewRepo_withSubmodules
是刚刚创建的本地 Git 存储库(当然没有“源”),则应该定义一个人工“远程源”(即使源指向自身),如果只允许相对 url要使用的其他子模块存储库。
Git 2.13 (Q2 2017) will improve the detection of the default origin of a submodule.
Git 2.13(2017 年第二季度)将改进对子模块默认来源的检测。
See commit d1b3b81(25 Feb 2017) by Stefan Beller (stefanbeller
).
(Merged by Junio C Hamano -- gitster
--in commit ae900eb, 10 Mar 2017)
请参阅Stefan Beller ( ) 的commit d1b3b81(2017 年 2 月 25 日)。(由Junio C Hamano合并-- --in commit ae900eb,2017 年 3 月 10 日)stefanbeller
gitster
submodule init
: warn about falling back to a local path
submodule init
: 警告回退到本地路径
As now documented:
正如现在记录的那样:
<repository>
is the URL of the new submodule's origin repository.
This may be either an absolute URL, or (if it begins with./
or../
), the location relative to the superproject's default remote repository
(Please note that to specify a repository 'foo.git
' which is located right next to a superproject 'bar.git
', you'll have to use '../foo.git
' instead of './foo.git
' - as one might expect when following the rules for relative URLs - because the evaluation of relative URLs in Git is identical to that of relative directories).The default remote is the remote of the remote tracking branch of the current branch.
If no such remote tracking branch exists or theHEAD
is detached, "origin
" is assumed to be the default remote.
If the superproject doesn't have a default remote configured the superproject is its own authoritative upstream and the current. working directory is used instead.
<repository>
是新子模块的原始存储库的 URL。
这可以是绝对 URL,或者(如果以./
或开头../
),相对于超级项目默认远程存储库的位置
(请注意,要指定foo.git
位于超级项目“bar.git
”旁边的存储库“ ”,您将必须使用 '../foo.git
' 而不是 './foo.git
' - 正如遵循相对 URL 规则时所期望的那样 - 因为 Git 中相对 URL 的评估与相对目录的评估相同)。默认远程是当前分支的远程跟踪分支的远程。
如果没有这样的远程跟踪分支存在或被HEAD
分离,origin
则假定“ ”是默认远程。
如果超级项目没有配置默认远程,则超级项目是它自己的权威上游和当前。而是使用工作目录。
Git 2.20 (Q4 2018) improves local path support for submodules.
Git 2.20(2018 年第四季度)改进了对子模块的本地路径支持。
See commit e0a862f(16 Oct 2018) by Stefan Beller (stefanbeller
).
(Merged by Junio C Hamano -- gitster
--in commit 3fc8522, 06 Nov 2018)
请参阅Stefan Beller ( ) 的提交 e0a862f(2018 年 10 月 16 日)。(由Junio C Hamano合并-- --在commit 3fc8522,2018 年 11 月 6 日)stefanbeller
gitster
submodule helper
: convert relative URL to absolute URL if neededThe submodule helper
update_clone
called by "git submodule update
", clones submodules if needed.
As submodules used to have the URL indicating if they were active, the step to resolve relative URLs was done in the "submodule init
" step. Nowadays submodules can be configured active without calling an explicit init, e.g. via configuringsubmodule.active
.When trying to obtain submodules that are set active this way, we'll fallback to the URL found in the
.gitmodules
, which may be relative to the superproject, but we do not resolve it, yet:git clone https://gerrit.googlesource.com/gerrit cd gerrit && grep url .gitmodules url = ../plugins/codemirror-editor ... git config submodule.active . git submodule update fatal: repository '../plugins/codemirror-editor' does not exist fatal: clone of '../plugins/codemirror-editor' into submodule path '/tmp/gerrit/plugins/codemirror-editor' failed Failed to clone 'plugins/codemirror-editor'. Retry scheduled [...] fatal: clone of '../plugins/codemirror-editor' into submodule path '/tmp/gerrit/plugins/codemirror-editor' failed Failed to clone 'plugins/codemirror-editor' a second time, aborting [...]
To resolve the issue, factor out the function that resolves the relative URLs in "
git submodule init
" (in the submodule helper in theinit_submodule
function) and call it at the appropriate place in theupdate_clone
helper.
submodule helper
: 如果需要,将相对 URL 转换为绝对 URL
update_clone
"git submodule update
"调用的子模块助手,如果需要,克隆子模块。
由于子模块曾经使用 URL 指示它们是否处于活动状态,因此解析相对 URL 的步骤在“submodule init
”步骤中完成。如今,子模块可以在不调用显式初始化的情况下配置为活动状态,例如通过配置submodule.active
.当尝试获取以这种方式设置为活动的子模块时,我们将回退到 中找到的 URL,该 URL
.gitmodules
可能与超级项目相关,但我们尚未解析它:git clone https://gerrit.googlesource.com/gerrit cd gerrit && grep url .gitmodules url = ../plugins/codemirror-editor ... git config submodule.active . git submodule update fatal: repository '../plugins/codemirror-editor' does not exist fatal: clone of '../plugins/codemirror-editor' into submodule path '/tmp/gerrit/plugins/codemirror-editor' failed Failed to clone 'plugins/codemirror-editor'. Retry scheduled [...] fatal: clone of '../plugins/codemirror-editor' into submodule path '/tmp/gerrit/plugins/codemirror-editor' failed Failed to clone 'plugins/codemirror-editor' a second time, aborting [...]
要解决此问题,请分解出解析“
git submodule init
”(在init_submodule
函数的子模块助手中)中的相对 URL 的函数,并在助手中的适当位置调用它update_clone
。
回答by Simon Knapp
I was trying the same thing, and found the following 'seems to have worked:
我正在尝试同样的事情,发现以下“似乎有效:
I have (on windows):
我有(在窗户上):
D:/phd/analyses
/analysis1/ #This is an existing repository
/analysis2/ #another existing repository
/analysis3.tex
/analysis4.tex
...
/analysisN.tex
analysis1.tex ... analysisN.tex
contain ideas I've not worked on yet ('stubs, say), and analysis1/
and analysis2/
are things I am working on (and hence have code, tex, ... in them). Once I get around to working on the other analyses, they will get moved to their own folders and hence their own repositories.
analysis1.tex ... analysisN.tex
包含的想法,我没有工作的,但('存根,说),并analysis1/
和analysis2/
事情,我在工作(并因此具有代码,纺织,......在他们)。一旦我开始处理其他分析,他们将被移动到他们自己的文件夹,因此他们自己的存储库。
What I did was (in git bash in analyses):
我所做的是(在分析中的 git bash 中):
git init
git add *.tex
git remote add self .
git submodule add self:/analysis2/.git analysis2
git submodule add self:/analysis5/.git analysis5
git commit -m "Initial commit"
This seems to have worked.
这似乎奏效了。
D:/phd/analyses/.git/config
looks like it should, and .gitmodules
looks like:
D:/phd/analyses/.git/config
看起来应该,.gitmodules
看起来像:
[submodule "analysis2"]
path = analysis2
url = self:analysis2/.git
[submodule "analysis5"]
path = analysis5
url = self:analysis5/.git
Regards, Simon Knapp
问候,西蒙·纳普
回答by Mechanical snail
(I've summarized just the solution here. Credit goes to VonC.)
(我在这里只总结了解决方案。归功于 VonC。)
In the containing repository (say containing.git/
), git
interprets relative paths as relative to the origin
remote, which is not defined. We want it to be relative to the containing.git/
directory, so run
在包含存储库(例如containing.git/
)中,git
将相对路径解释为相对于origin
未定义的远程路径。我们希望它相对于containing.git/
目录,所以运行
git remote add origin ..
(Not sure why it's ..
rather than .
.)
(不知道为什么是..
而不是.
。)
Now you can add the sub-module:
现在您可以添加子模块:
git submodule add ../extern/Lib1 lib