没有子模块的嵌套 git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2317652/
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
Nested git repositories without submodules?
提问by stevo
It seems a git repo inside a parent repo isn't included in a commit on the parent unless it's setup as a submodule. Is it possible to override this behaviour and treat the nested git repo as any other directory? I don't want to rely on external dependencies through submodules but want to use git to manage these dependencies within the project.
除非将父存储库中的 git 存储库设置为子模块,否则它似乎不包含在父存储库中的提交中。是否可以覆盖此行为并将嵌套的 git repo 视为任何其他目录?我不想通过子模块依赖外部依赖,而是想在项目中使用 git 来管理这些依赖。
采纳答案by VonC
1/ You could override that through:
1/您可以通过以下方式覆盖它:
- either a git configsetting: set the environment variable
$GIT_DIR
you define your.git
directory of the nested Git working tree as an external.git
(external to both the nested repo and the main repo) - or by setting your nested repo 'N' outsidethe main repo, but checkout that repo 'N' insidethe main repo:
- 无论是混帐配置设置:设置环境变量
$GIT_DIR
定义你的.git
嵌套的Git工作树的目录作为外部.git
(外部嵌套回购和主回购两者) - 或者通过在主存储库之外设置您的嵌套存储库“N” ,但在主存储库内签出该存储库“N” :
core.worktree
Set the path to the root of the work tree. This can be overridden by the GIT_WORK_TREE
environment variable and the --work-tree
command line option
将路径设置为工作树的根。这可以被GIT_WORK_TREE
环境变量和--work-tree
命令行选项覆盖
In both case, the idea is to have a nested worktree without any .git
subdirectory in it.
在这两种情况下,想法都是有一个嵌套的工作树,其中没有任何.git
子目录。
2/ With submodules, the nested git repo is not really included in the parent repo.
A special entryis made in the parent tree to record the external Git SHA1.
2/ 对于子模块,嵌套的 git repo 并没有真正包含在父 repo 中。在父树中创建
一个特殊条目来记录外部 Git SHA1。
new file mode 160000
index 0000000..4c4c5a2
See also "nature of Git submodules" (third part of the answer)
另请参阅“ Git 子模块的性质”(答案的第三部分)
Nine years later, this discussionis quite clear:
九年后,这个讨论已经很清楚了:
I don't want to use submodules or crutches such as renaming all
.git/
in subdirectories.
I just want that Git treats my.git/
subdirs as plain dirs with any other names.
我不想使用子模块或拐杖,例如重命名
.git/
子目录中的所有内容。
我只是希望 Git 将我的.git/
子目录视为具有任何其他名称的普通目录。
Brian m. Carlson(bk2204
) answers:
布赖恩·M。卡尔森( bk2204
) 回答:
This is not possible.
You can't add a non-bare repository as a part of a parent repository without using submodules.
Git uses the.git
directory to find the working tree and for safety reasons doesn't allow files or directories named that to be checked in.
Allowing users to check in.git
directories would allow configuration and hooks to be stored as part of the repository, which would allow the execution of arbitrary code if someone cloned it and then changed into the subrepository.
这不可能。
您不能在不使用子模块的情况下将非裸存储库添加为父存储库的一部分。
Git 使用该.git
目录来查找工作树,并且出于安全原因不允许签入命名的文件或目录。
允许用户签入.git
目录将允许将配置和挂钩存储为存储库的一部分,这将允许如果有人克隆它然后更改到子存储库中,则执行任意代码。
回答by Drew LeSueur
I found another method that seems to work for me
我找到了另一种似乎对我有用的方法
If you git add somefolder/
<-- make sure you have the /
on the end, Then it will add all the files instead of treating it like a submodule.
如果您git add somefolder/
<-- 确保/
最后有 ,那么它将添加所有文件而不是将其视为子模块。
More is mentioned here: http://debuggable.com/posts/git-fake-submodules:4b563ee4-f3cc-4061-967e-0e48cbdd56cb
这里提到了更多:http: //debuggable.com/posts/git-fake-submodules: 4b563ee4-f3cc-4061-967e-0e48cbdd56cb
回答by imz -- Ivan Zakharyaschev
See also Are git submodules the only safe way to have working copies within working copies?.
另请参阅git 子模块是在工作副本中拥有工作副本的唯一安全方法吗?.
Actually, when I came across the current discussion, I was concerned with a problem different from the on solved here: using git as a kind of archiving tool that could archive a filesystem tree which already has some git working directories... perhaps the other question is closer to my problem.
实际上,当我遇到当前的讨论时,我关心的是一个不同于这里解决的问题:使用 git 作为一种归档工具,可以归档已经有一些 git 工作目录的文件系统树......也许另一个问题更接近我的问题。
回答by funroll
This isn't the most elegant solution, but it's worth mentioning.
这不是最优雅的解决方案,但值得一提。
If you zip up the folder containing the repository you can check that into the outer repository.
如果您压缩包含存储库的文件夹,您可以将其检入外部存储库。
This can be helpful because you'll have the full complete state including git reflog
.
这会很有帮助,因为您将拥有完整的状态,包括git reflog
.
回答by MatPag
One of the most simple (and dirty) thing you can do is renaming the .git
folder
您可以做的最简单(和肮脏)的事情之一是重命名.git
文件夹
I had this folder structure:
我有这个文件夹结构:
|- .git
|- folders
|- second-repo
|- .git
|- folders
I simply used this bash command from root folder:
我只是从根文件夹中使用了这个 bash 命令:
mv /.git /.not_git
Now you can go inside the second-repo
and use its own git for commands and in this way you can go back and forth to use both git repos.
现在你可以进入second-repo
并使用它自己的 git 命令,这样你就可以来回使用两个 git repos。
PS: Git subomodules is the correct way to handle this but in my case I couldn't go for them
PS:Git subomodules 是处理此问题的正确方法,但在我的情况下,我无法使用它们