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

标签 git git-submodules git-mv

我有以下 git 结构

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

我想将 git 子模块 c 移动到文件夹 2015。 我知道执行此操作的“肮脏方法”(涉及修改 .git/config 和更改 .git/modules 文件中的几个文件中的 gitdir)

我最近读到 git mv 应该能够做到这一点,即运行

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 status 上)

是否有人知道执行此移动的干净方法(即不涉及手动更改 .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 (2018 年 3 月 28 日)作者:Jonathan Tan ( jhowtan ) .
参见 commit da62f78 , commit 0c89fdd , commit 3b8fb39 , commit f793b89 , commit 61aad92 (2018 年 3 月 28 日)作者:Stefan Beller ( stefanbeller ) .
(由 Junio C Hamano -- gitster -- merge 于 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 年第四季度更新:

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

参见 commit c514167 (2017 年 9 月 15 日)作者 Heiko Voigt ( hvoigt ) .
(由 Junio C Hamano -- gitster -- merge 于 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

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

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注入(inject)正确的路径:

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\a2\2015\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\a2\2015\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

关于Git:递归移动子模块(嵌套子模块),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32782382/

相关文章:

cmake - 可选的 add_subdirectory(如 find_package)

javascript - 如何创建与环境无关的 javascript 库

git - git 如何处理将另一个存储库克隆到子目录中?

git - 在 Git 中处理文件重命名

git mv 和变化和相似度索引

git 钩子(Hook) : ensure each merge into the master has a message also the automatic merges

git:如何还原唯一更改是修改时间戳的所有文件?

svn - 是否可以在 subversion 中将 git 存储库作为 "vendor branch"?

java - 使用 Eclipse 插件检测 git 存储库中的代码气味?