【问题标题】:how can I add git submodule into git repo as normal directory?如何将 git 子模块作为普通目录添加到 git repo 中?
【发布时间】:2016-09-30 12:26:41
【问题描述】:

让我们看看它是什么

  • 创建两个 git repo sub & test
mkdir 测试子 cd test && git init && touch README && git add README && git commit -m "initialize the git repo" && cd .. cd sub && git init && touch README && git add README && git commit -m "initialize the sub git repo" && cd ..
  • sub 存储库移动到test
mv 子测试 光盘测试 git添加子 git commit -m "添加子目录"

我想把它们当成一个git repo远程推送,但是sub目录下的文件现在不能包含了?

如何以简单的方式实现这一点,例如将 sub 视为普通目录?

用例

我尝试使用 Dockerfile 将我的 jenkins 数据文件夹 (JENKINS_HOME) 添加到 docker 映像中以进行演示。 (ADD JEKINS_HOME /opt/jenkins)

JENKINS_HOME
Dockerfile

我的 jenkin 有 scriptler plugin,其中包含用于其目的的 git repo。然后它存在于我的 docker image git repo 中,如下所示

$ 找到詹金斯码头工人 ./.git ./.git/..(跳过 ./Dockerfile ./JENKINS_HOME ./JENKINS_HOME/scriptler ./JENKINS_HOME/scriptler/scripts ./JENKINS_HOME/scriptler/scripts/.git ./JENKINS_HOME/scriptler/scripts/.git/...(跳过) ./JENKINS_HOME/scriptler/scripts/Sample.groovy ./JENKINS_HOME/...(跳过) ./自述文件

【问题讨论】:

    标签: git dockerfile jenkins-scriptler


    【解决方案1】:

    看来你做不到。 .git 这个名字在源代码中是硬编码的:https://github.com/git/git/blob/fe9122a35213827348c521a16ffd0cf2652c4ac5/dir.c#L1260

    可能的一种方法是制作一个脚本,将 .git 重命名为其他名称,然后在将其添加到 repo 之前和之后返回

    scripts下的工作目录中

    mv .git hidden-git
    

    在 Dockerfile 中

    RUN mv $JENKINS_HOME/scriptler/scripts/hidden-git     
    $JENKINS_HOME/scriptler/scripts/.git
    

    或者,也许可以将GIT_DIR 环境变量传递给插件,因此它可以使用另一个名称。

    【讨论】:

    • 更改该插件的行为会很好。脚本可能还不错。
    • @LarryCai 如果我正确阅读了这段代码,它不支持 GIT_DIR 自定义......所以除非修改代码,否则你无法做到。 github.com/jenkinsci/git-server-plugin/blob/… 它应该有 setGitDir 和一些可自定义的值,但这里什么都没有。
    • @LarryCai 是开源的,欢迎大家介绍补丁。
    • 可能是。但我这次选择了脚本,直接更新你的答案。
    【解决方案2】:
    git add sub
    git commit -m "add sub directory"
    

    我想把它们当作一个git repo远程推送,但是现在子目录下的文件不能被包含?

    它们不包括在内,因为 test 看到 repo sub as nested git repo, and records only its gitlink, or SHA1,而不是它的 url,如果 sub 被添加为子模块。

    您需要先将sub 推送到远程网址,然后将其添加为test 的子模块以查看sub 文件。

     cd test
     git submodule add -- /url/to/sub
    

    或者你需要to use a subtree

    cd test
    git subtree --prefix sub /url/to/sub master --squash
    

    【讨论】:

    • 谢谢,用例不太适合这个解决方案
    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 2023-03-29
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多