Git:递归移动子模块(嵌套子模块) [英] Git: moving submodules recursively (nested submodules)

查看:96
本文介绍了Git:递归移动子模块(嵌套子模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下git结构

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

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

I would like to move the git submodule c to the folder 2015. I know of "dirty ways" to do this (that involve modifying .git/config and changing gitdir in several of the files in the .git/modules files)

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

I recently read that git mv should be able to do this, i.e., running

git mv c 2015/

这对于没有嵌套子模块(在我的情况下为d)的存储库来说效果很好.但是,当我在目录上运行此命令时,出现了

This works fine for repositories in which there is no nested submodule (d in my case). However, when I run this command on my directory, I'm getting errors like

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

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

(note, this error occurs on a git status after executing the above mentioned move)

有人知道这样做的一种干净方法(即,不涉及手动更改.git/modules文件中的路径的方法)吗?

Is anyone aware of a clean method to do this move (i.e., one that does not involve manually changing paths in .git/modules files)?

修改:(2015年6月10日)

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

My current best solution that does not involve modifications to any of the git config files is (first make sure that all changes to d have been committed and pushed somewhere)

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

修改:(2015年8月10日)

一种更具侵入性的解决方法

An even less intrusive workaround

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

(21/9/2018)

从git版本2.19开始.此问题已得到解决,git mv的行为符合预期.

Since git version 2.19. This has been fixed and git mv behaves as expected.

推荐答案

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

Update Q2 2018 and Git 2.18:

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

Moving a submodule that itself has submodule in it with "git mv" forgot to make necessary adjustment to the nested sub-submodules;
now the codepath learned to recurse into the submodules.

请参见提交6856077 (2018年3月28日)由提交da62f78 提交f793b89 Junio C Hamano-gitster-

See commit 6856077 (28 Mar 2018) by Jonathan Tan (jhowtan).
See commit da62f78, commit 0c89fdd, commit 3b8fb39, commit f793b89, commit 61aad92 (28 Mar 2018) by Stefan Beller (stefanbeller).
(Merged by Junio C Hamano -- gitster -- in commit 0c7ecb7, 08 May 2018)

子模块:在移动子模块后修复嵌套的子模块

由于子模块本身可以具有嵌套的子模块,因此我们也想在被要求时修复嵌套的子模块.添加一个选项以递归到嵌套的子模块中,并进行连接.

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.

由于子模块由其名称标识(确定子模块的git 内部目录(相对于其超级项目的git目录) 并通过他们在超级项目的工作树中的路径,我们需要 确保name <-> path的映射保持完整.我们能做的 首先写出.gitmodules文件在git-mv命令中 然后强制重新加载子模块配置机制.

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年第四季度更新:


Update Q4 2017:

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

The latest Git 2.14.x/2.15 (Q4 2017) documents the bug

请参见提交c514167 (2017年9月15日)通过 Junio C Hamano-gitster-

See commit c514167 (15 Sep 2017) by Heiko Voigt (hvoigt).
(Merged by Junio C Hamano -- gitster -- in commit 450b908, 25 Sep 2017)

git-mv与子模块一起使用时,它将检测到并更新 其配置的路径(.gitmodules,工作树和gitfile).
这不适用于用户重命名根目录的递归子模块 子模块.

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


Original answer 2015

我刚刚使用git 2.6.0(在Windows上)对其进行了测试,并且移动具有自身嵌套子模块的子模块似乎是有问题的:

I just tested it with git 2.6.0 (on Windows), and moving submodules with nested submodules of their own seems to be problematic:

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:

If I try and move submodule c in the 2015 subfolder:

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

我发现我需要修改嵌套模块d 中的2件事:

I found that I need to modify 2 things in the nested module d:

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

1/ modify manually .git/modules/c/modules/d/config to inject the right path:

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:

From there, a git status works, but I like (to be sure) to add a 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确实显示了预期的结果:

The git status does show the expected result:

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>

我现在克隆该仓库以检查移动是否确实正确注册:

I now clone that repo to check the move was indeed properly registered:

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/子文件夹中:

As you can see, c and c/d are in the expected 2015/ subfolder:

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:递归移动子模块(嵌套子模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆