Git:递归移动子模块(嵌套子模块)

Git: moving submodules recursively (nested submodules)

我有以下git结构

- git-repo a
-- subdirectory 2015
--- git-submodule b
-- git-submodule c
--- git-submodule d

我想将 git 子模块 c 移动到文件夹 2015。 我知道 "dirty ways" 可以执行此操作(涉及修改 .git/config 和更改 .git/modules 文件中的几个文件中的 gitdir)

我最近读到 git mv 应该可以做到这一点,即 运行ning

git mv c 2015/

这适用于没有嵌套子模块(在我的例子中是 d)的存储库。但是,当我 运行 在我的目录中执行此命令时,我收到类似

的错误
fatal: Not a git repository: d/../../.git/modules/c/modules/d
fatal: 'git status --porcelain' failed in submodule 2015/c

(注意,执行上述移动后,此错误发生在 git 状态)

是否有人知道执行此移动的干净方法(即不涉及手动更改 .git/modules 文件中的路径的方法)?

编辑:(6/10/2015)

我目前不涉及修改任何 git 配置文件的最佳解决方案是(首先确保对 d 的所有更改都已提交并推送到某处)

rm c/d -rf
git mv c 2015
cd 2015/c
git submodule update

编辑:(8/10/2015)

侵入性更小的解决方法

git mv c 2015
rm 2015/c/d/.git
cd 2015/c
git submodule update

编辑:(21/9/2018)

自 git 版本 2.19。这已得到修复,git mv 的行为符合预期。

更新 2018 年第二季度和 Git 2.18:

用“git mv”移动本身有子模块的子模块忘记对嵌套的子模块进行必要的调整;
现在代码路径学会了递归到子模块中

参见 commit 6856077 (28 Mar 2018) by Jonathan Tan (jhowtan)
参见 commit da62f78, commit 0c89fdd, commit 3b8fb39, commit f793b89, commit 61aad92 (28 Mar 2018) by Stefan Beller (stefanbeller)
(由 Junio C Hamano -- gitster -- in commit 0c7ecb7 合并,2018 年 5 月 8 日)

submodule: fixup nested submodules after moving the submodule

As submodules can have nested submodules themselves, we'd also want to fix the nested submodules when asked to. Add an option to recurse into the nested submodules and connect them as well.

As submodules are identified by their name (which determines their git directory in relation to their superproject's git directory) internally and by their path in the working tree of the superproject, we need to make sure that the mapping of name <-> path is kept intact. We can do that in the git-mv command by writing out the .gitmodules file first and then forcing a reload of the submodule config machinery.


2017 年第 4 季度更新:

最新的 Git 2.14.x/2.15(2017 年第 4 季度)记录了该错误

参见 commit c514167 (15 Sep 2017) by Heiko Voigt (hvoigt)
(由 Junio C Hamano -- gitster -- in commit 450b908 合并,2017 年 9 月 25 日)

When using git-mv with a submodule it will detect that and update the paths for its configurations (.gitmodules, worktree and gitfile).
This does not work for recursive submodules where a user renames the root submodule.


2015 年原答案

我刚刚用 git 2.6.0(在 Windows 上)测试了它,移动带有自己嵌套子模块的子模块似乎有问题:

C:\Users\vonc\prog\git\tests\submove>git clone --recursive a a1
Cloning into 'a1'...
done.
Submodule '2015/b' (C:/Users/vonc/prog/git/tests/submove/b) registered for path '2015/b'
Submodule 'c' (C:/Users/vonc/prog/git/tests/submove/c) registered for path 'c'
Cloning into '2015/b'...
done.
Submodule path '2015/b': checked out 'dc18955ec7b9ad0c04245968e2474646e4d593b2'
Cloning into 'c'...
done.
Submodule path 'c': checked out 'fb4722eaca17ac171b7a2c8c5a1ac1e697f0ee85'
Submodule 'd' (C:/Users/vonc/prog/git/tests/submove/d) registered for path 'd'
Cloning into 'd'...
done.
Submodule path 'c/d': checked out '73cd7b8ff82519720b2fcca18df5ed00dd618b71'

我有:

a1
  c
    d
  2015
    b

如果我尝试移动 2015 子文件夹中的子模块 c

C:\Users\vonc\prog\git\tests\submove\a1>git mv c 2015/c

C:\Users\vonc\prog\git\tests\submove\a1>git status
fatal: Not a git repository: d/../../.git/modules/c/modules/d
fatal: 'git status --porcelain' failed in submodule 2015/c

我发现我需要修改嵌套模块中的 2 个东西 d:

1/手动修改.git/modules/c/modules/d/config注入正确路径:

git config -f .git/modules/c/modules/d/config core.worktree ../../../../../2015/c/d
                                                                           ^^^^

2/修改d工作树:

git config -f .git/modules/c/modules/d/config core.worktree ../../../../../2015/c/d

从那里开始,git status 有效,但我喜欢(可以肯定)添加 git submodule sync --recursive:

C:\Users\vonc\prog\git\tests\submove\a1>git submodule sync --recursive
Synchronizing submodule url for '2015/b'
Synchronizing submodule url for '2015/c'
Synchronizing submodule url for '2015/c/d'

git status 确实显示了预期的结果:

C:\Users\vonc\prog\git\tests\submove\a1>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitmodules
        renamed:    c -> 2015/c

我添加并提交更改:

C:\Users\vonc\prog\git\tests\submove\a1>git add .

C:\Users\vonc\prog\git\tests\submove\a1>git commit -m "move sub c in 2015/c"
[master 8289632] move sub c in 2015/c
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename c => 2015/c (100%)

C:\Users\vonc\prog\git\tests\submove\a1>gl
* 8289632 - (HEAD -> master) move sub c in 2015/c (3 seconds ago) <VonC>
* 7ebb8e0 - (origin/master, origin/HEAD) a with sub c (38 minutes ago) <VonC>

我现在克隆那个 repo 以检查移动是否确实正确注册:

C:\Users\vonc\prog\git\tests\submove>git clone --recursive a1 a2
Cloning into 'a2'...
done.
Submodule '2015/b' (C:/Users/vonc/prog/git/tests/submove/b) registered for path '2015/b'
Submodule 'c' (C:/Users/vonc/prog/git/tests/submove/c) registered for path '2015/c'
Cloning into '2015/b'...
done.
Submodule path '2015/b': checked out 'dc18955ec7b9ad0c04245968e2474646e4d593b2'
Cloning into '2015/c'...
done.
Submodule path '2015/c': checked out 'fb4722eaca17ac171b7a2c8c5a1ac1e697f0ee85'
Submodule 'd' (C:/Users/vonc/prog/git/tests/submove/d) registered for path 'd'
Cloning into 'd'...
done.
Submodule path '2015/c/d': checked out '73cd7b8ff82519720b2fcca18df5ed00dd618b71'

如您所见,cc/d 位于预期的 2015/ 子文件夹中:

C:\Users\vonc\prog\git\tests\submove>cd a2

C:\Users\vonc\prog\git\tests\submove\a2>dir 2015\c

 Directory of C:\Users\vonc\prog\git\tests\submove\a215\c

03/10/2015  18:10    <DIR>          .
03/10/2015  18:10    <DIR>          ..
03/10/2015  18:10                29 .git
03/10/2015  18:10                40 .gitmodules
03/10/2015  18:10    <DIR>          d
               2 File(s)             69 bytes
               3 Dir(s)  23 656 910 848 bytes free

C:\Users\vonc\prog\git\tests\submove\a2>dir 2015\c\d

 Directory of C:\Users\vonc\prog\git\tests\submove\a215\c\d

03/10/2015  18:10    <DIR>          .
03/10/2015  18:10    <DIR>          ..
03/10/2015  18:10                42 .git
03/10/2015  18:10                 3 d.txt
               2 File(s)             45 bytes
               2 Dir(s)  23 656 910 848 bytes free

@VonC 在 中确认,这是 git mv 中的错误。

有几种可能的解决方法。最简单的一个不需要对 .git 文件进行复杂的修改(自问这个问题以来我一直在使用这个)。它的工作原理如下:

git mv c 2015
rm 2015/c/d/.git
cd 2015/c
git submodule update

它暂时删除了 d 子子模块中的 .git 文件。 git 子模块更新然后再次修复此 .git 文件。

有关避免临时删除 git 目录的其他解决方法,请参阅此答案: