为什么`git submodules add`将GIT信息存储在父.git中?

问题描述 投票:0回答:1

执行

git submodule add
只是创建一个“.git”文件,该文件指向包含数据的父 GIT 文件夹。示例:

为什么子模块存储在父模块中?如何防止 GIT 这样做?我想像我拥有的其他模块一样保持一致的结构,其中每个项目都有自己的“.git”文件夹,位于存储项目数据的位置旁边。

git repository git-submodules checkout
1个回答
2
投票

一开始子模块按照您描述的方式工作。但后来 Git 开发人员决定更改为当前使用

.git
文件(名为 gitlink)的方式,并将
.git/
目录推送到超级项目的
.git/modules/
下。为了修复现有的存储库,他们创建了
git submodule absorbgitdirs
。所以你可以看到他们尽最大努力以当前的方式做事,并且无法返回到子模块中存储
.git/
目录的原始方式。您可以手动将其移回并修复
.git/config
。这就是我设法为一个子模块做到这一点的方法:

#! /bin/sh
set -e

# To the top-level directory of the current submodule
cd "`git rev-parse --show-toplevel`"

unset GIT_DIR

# If .git/ subdirectory is already here
test -d .git && exit 0

if ! test -f .git; then
    echo "Error: Cannot find gitlink, aborting" >&2
    exit 1
fi

# Fix core.worktree now
git config --unset core.worktree

read _gitdir gitpath < .git
unset _gitdir
rm .git
exec mv "$gitpath" .git

我无法从

git submodule foreach --recursive
运行它,因为命令从上到下运行,而我的脚本只能从下到上运行(它不能修复孩子的
.git
链接)。

更新。这是递归变体:

#! /bin/sh
# The script cannot be run with `git submodule foreach --recursive`
# because the command runs recursively from top to bottom
# while the command is required to be run from bottom to top
# because it doesn't fix childrens' gitlinks.
# So the script runs recursion itself;
# it can be run with `git submodule foreach` without `--recursive`.
set -e

START_DIR="`pwd`"
cd "`dirname \"$0\"`"
PROG_DIR="`pwd`"
cd "$START_DIR"

# To the top-level directory of the current submodule or the superproject
cd "`git rev-parse --show-toplevel`"

unset GIT_DIR

# If .git/ subdirectory is already here
test -d .git && exit 0

if ! test -f .git; then
    echo "Error: Cannot find gitlink, aborting" >&2
    exit 1
fi

if test -f .gitmodules; then
    git submodule foreach "$PROG_DIR"/"`basename \"$0\"`"
fi

# Fix core.worktree now
git config --unset core.worktree

read _gitdir gitpath < .git
unset _gitdir
rm .git
exec mv "$gitpath" .git

或者您可以安装非常旧的 Git,以旧方式克隆子模块,然后使用较新的 Git 照常工作。

© www.soinside.com 2019 - 2024. All rights reserved.