Opposite/reverse 个 "git submodule absorbgitdirs"?

Opposite/reverse of "git submodule absorbgitdirs"?

如何执行 git submodule absorbgitdirs 的反向操作? IE。将子模块的 .git 信息移出 superproject/.git/modules/<module> 并移回 superproject/path/to/<module>/.git 目录?

我仍然希望 <module> 成为 superproject 的子模块,我只是不想 <module>.git 目录信息在 superproject 中的 .git/modules 目录。

请注意,这会使 superproject/path/to/<module>/ 成为 嵌套 Git 存储库,其 SHA1 仍将由父项目记录。

要保持​​完全相同的状态,您可以复制 superproject/.git/modules/<module> 并重命名为 superproject/path/to/<module>,并将 <module>/<module> 重命名为 <module>/.git

然后你可以使用git submodule deinit删除子模块:

mv asubmodule asubmodule_tmp
git submodule deinit -f -- a/submodule    
rm -rf .git/modules/a/submodule

# if you want to leave it in your working tree
git rm --cached asubmodule
mv asubmodule_tmp asubmodule

I still want to be a submodule of superprojec

那么它的 .git 文件夹将在 superproject/.git/modules/<module>

submodule absorbgitdirs不留任何选择:

If a git directory of a submodule is inside the submodule, move the git directory of the submodule into its superprojects $GIT_DIR/modules path and then connect the git directory and its working directory by setting the core.worktree and adding a .git file pointing to the git directory embedded in the superprojects git directory.

我在 git config 中没有看到任何可能移动 $GIT_DI R/modules 的配置。

absorbgitdirs 是在 commit f6f8586 中为 Git 2.12(2016 年第 4 季度)引入的
Its tests shows 它期望使用 GIT_DIR/modules.

较早的 Git(before Git 1.7.8,2011 年 10 月)直接在子模块文件夹中有一个 .git


正如 Jeremiah Rose in 所述:

Another use case is: if you are using a Docker container to use git commands inside a submodule, where the container can't see the superproject and errors out. :(

我写了一个脚本来做到这一点。将此添加到您的 ~/.gitconfig:

[alias]
    extract-submodules = "!gitextractsubmodules() { set -e && { if [ 0 -lt \"$#\" ]; then printf \"%s\n\" \"$@\"; else git ls-files --stage | sed -n \"s/^160000 [a-fA-F0-9]\+ [0-9]\+\s*//p\"; fi; } | { local path && while read -r path; do if [ -f \"${path}/.git\" ]; then local git_dir && git_dir=\"$(git -C \"${path}\" rev-parse --absolute-git-dir)\" && if [ -d \"${git_dir}\" ]; then printf \"%s\t%s\n\" \"${git_dir}\" \"${path}/.git\" && mv --no-target-directory --backup=simple -- \"${git_dir}\" \"${path}/.git\" && git --work-tree=\"${path}\" --git-dir=\"${path}/.git\" config --local --path --unset core.worktree && rm -f -- \"${path}/.git~\" && if 1>&- command -v attrib.exe; then MSYS2_ARG_CONV_EXCL=\"*\" attrib.exe \"+H\" \"/D\" \"${path}/.git\"; fi; fi; fi; done; }; } && gitextractsubmodules"

然后 运行:

git extract-submodules [<path>...]

如果您 运行 它没有指定任何子模块,它将提取所有子模块。


如果您想查看代码的作用,只需更改

&& gitextractsubmodules

最后到

&& type gitextractsubmodules

然后运行不带参数的命令使Bash漂亮地打印函数体。