I have the following git structure
- git-repo a
-- subdirectory 2015
--- git-submodule b
-- git-submodule c
--- git-submodule d
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)
I recently read that git mv should be able to do this, i.e., running
git mv c 2015/
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
(note, this error occurs on a git status after executing the above mentioned move)
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)?
Edit: (6/10/2015)
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
Edit: (8/10/2015)
An even less intrusive workaround
git mv c 2015
rm 2015/c/d/.git
cd 2015/c
git submodule update
Edit: (21/9/2018)
Since git version 2.19. This has been fixed and git mv
behaves as expected.
As confirmed by @VonC in https://stackoverflow.com/a/32924692/2274140, this is a bug in git mv.
There are several possible workarounds. The easiest one requires no complex modifications of .git files (I have been using this one ever since asking the question). It works as follows:
It temporarily removes the .git file in the
d
subsubmodule. The git submodule update then fixes this .git file again.For other workarounds that avoid the temporary remove of the gitdir, see this answer: https://stackoverflow.com/a/32924692/2274140
Update Q2 2018 and Git 2.18:
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.
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)Update Q4 2017:
The latest Git 2.14.x/2.15 (Q4 2017) documents the bug
See commit c514167 (15 Sep 2017) by Heiko Voigt (
hvoigt
).(Merged by Junio C Hamano --
gitster
-- in commit 450b908, 25 Sep 2017)Original answer 2015
I just tested it with git 2.6.0 (on Windows), and moving submodules with nested submodules of their own seems to be problematic:
I have:
If I try and move submodule
c
in the 2015 subfolder:I found that I need to modify 2 things in the nested module
d
:1/ modify manually
.git/modules/c/modules/d/config
to inject the right path:2/ modify
d
worktree:From there, a
git status
works, but I like (to be sure) to add agit submodule sync --recursive
:The
git status
does show the expected result:I add and commit that change:
I now clone that repo to check the move was indeed properly registered:
As you can see,
c
andc/d
are in the expected2015/
subfolder: