【问题标题】:Convert git repo to submodule将 git repo 转换为子模块
【发布时间】:2021-03-20 20:36:14
【问题描述】:

所以我对整个git 主题还很陌生。但我想做的是在多台机器上同步我的dotfiles。我知道那里有很多教程,并且我掌握了基本概念。但我的问题更具体。

第一件事:我正在使用 Windows(目前)和Vim。最重要的是,我使用vundle 来管理我的插件。现在插件本身就是git-repos。目录~/.vim(这是我要同步的主仓库)包含它们。

所以我的问题是,如何初始化现有仓库的submodule

我现在得到的是~/.vim/bundle/... 中的一堆绿色文件夹,它们代表其他存储库。 git status 命令将它们报告为未跟踪的内容(不,这不是我的.gitignore 做一些时髦的事情)。

如果你不熟悉绿色文件夹“phenomenon”,你可能想看看我的 repo。

附:其中一个子文件夹显示 modified content 而不是 untracked content,即使我没有碰它。我不知道为什么。

【问题讨论】:

    标签: git git-submodules


    【解决方案1】:

    如果你所有的包都只是基本的克隆,没有修改,你可以这样做:从你的 dotfiles repo 的根目录:

    for d in `find .vim/bundle/ -maxdepth 1 -mindepth 1 -type d`
    do
        pushd $d
        export url=$(git config --get remote.origin.url)
        popd
        git submodule add $url $d
    done
    

    这应该可以很好地工作 - 如果任何包不是存储库,或者如果您手动添加了它,那么它只会传递该目录。

    然后您可以执行 git submodule foreach git pull 来更新它们,并添加它们以更新您的 dotfiles repo 的引用。

    【讨论】:

    • 这正是我所需要的。谢谢。顺便说一句,在尝试添加子模块之前,我还添加了一些检查以确保子模块是 git repo。这样我就可以在一个文件夹中使用它,其中一些子文件夹是 repos 而有些不是
    【解决方案2】:

    每次您看到未经任何明确修改的修改内容时,请检查全局设置 (core.autocrlf, core.filemode),它可以在结帐时进行更改。)

    对于.vim/bundle/ 中的每个目录,您需要:

    • 转到该模块:cd .vim/bundle/aModule
    • 初始化 git repo (git init .),提交并推送到 GitHub 上的新 git repo
    • 回到父仓库:cd ..(你在.vim/bundle/
    • git submodule add https:///github.com/tairun/aModule aModule(注意这个子模块可以track the latest of a branch, if you want to

    然后git submodule update --init --remote

    【讨论】:

    • 感谢您的回答。需要一些时间来测试一切。到目前为止,一切都很顺利,尽管我没有完全按照你告诉我的去做。我对很多像git submodule update --init --remote 这样的链式命令仍然不熟悉,所以我只能猜测它们的作用。我可能稍后再回来分享我的步骤。但基本上我所做的是(从我的回购顶部)git submodule add git@github.com:user/repo.git bundle/aBundle。当我收到index 错误时,我运行了git rm --cache bundle/aBundle。从那里我会做一个 git submodule update --init.
    • @Sensei:您可能应该将其作为单独的答案发布。这个问题可能并不完全清楚,您的 vundle 插件是 git repos 从网络克隆,并且您正试图用基本的子模块替换它们。
    【解决方案3】:

    因为工作对我来说似乎是重复的,所以我想分享以下相当长的 git 别名:

    git config --global alias.convertGitRepoToSubmodule '!f() { if [ "$#" = 0 ]; then echo "Usage: convertGitRepoToSubmodule <submodule_directory...>" >&2; return 1; fi; cd -- "${GIT_PREFIX:-.}"; for i; do origin="$(cd "$i" && git config --get remote.origin.url)"; ( set -x; git submodule add "$origin" "$i"; ); done; }; f'
    

    那就这样吧:

    git convertgitrepotosubmodule $(find .vim/bundle/ -maxdepth 1 -mindepth 1 -type d)
    

    它应该在所有目录上调用git submodule add &lt;the origin&gt; &lt;the dir&gt;

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 2015-07-31
      • 2020-03-14
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      相关资源
      最近更新 更多