I would like to change the directory name of a Git submodule in my Git superproject.
Lets suppose I have the following entry in my .gitmodules
file:
The most modern answer, taken from Valloric's comment above:
git mv old/submod new/submod
git status
.)git commit
and you're good to go!Done!
[Update: 2014-11-26] As Yar summarizes nicely below, before you do anything, make sure you know the URL of the submodule. If unknown, open .git/.gitmodules and examine the keysubmodule.<name>.url.
What worked for me was to remove the old submodule using git submodule deinit <submodule>
followed by git rm <submodule-folder>
. Then add the submodule again with the new folder name and commit. Checking git status before committing shows the old submodule renamed to the new name and .gitmodule modified.
$ git submodule deinit foo
$ git rm foo
$ git submodule add https://bar.com/foo.git new-foo
$ git status
renamed: foo -> new-foo
modified: .gitmodules
$ git commit -am "rename foo submodule to new-foo"
The string in quotes after "[submodule" doesn't matter. You can change it to "foobar" if you want. It's used to find the matching entry in ".git/config".
Therefore, if you make the change before you run "git submodule init", it'll work fine. If you make the change (or pick up the change through a merge), you'll need to either manually edit .git/config or run "git submodule init" again. If you do the latter, you'll be left with a harmless "stranded" entry with the old name in .git/config.
I just went through this ordeal yesterday and this answer worked perfectly. Here are my steps, for clarity:
more .gitmodules
because once you delete the submodule it's not going to be arounddeinit
, rm
and then submodule add
EXAMPLE
COMMANDS
git submodule deinit Classes/lib/mustIReally
git rm foo
git submodule add http://developer.audiob.us/download/SDK.git lib/AudioBus
# do your normal commit and push
git commit -a
NOTE: git mv doesn't do this. At all.
Note: As mentioned in the comments this answer refers to the steps needed with older versions of git. Git now has native support for moving submodules:
Since git 1.8.5,
git mv old/submod new/submod
works as expected and does all the plumbing for you. You might want to use git 1.9.3 or newer, because it includes fixes for submodule moving.
The process is similar to how you'd remove a submodule (see How do I remove a submodule?):
.gitmodules
and change the path of the submodule appropriately, and put it in the index with git add .gitmodules
.mkdir -p new/parent
).mv -vi old/parent/submodule new/parent/submodule
).git add new/parent
).git rm --cached old/parent/submodule
..git/modules/old/parent/submodule
with all its content to .git/modules/new/parent/submodule
..git/modules/new/parent/config
file, make sure that worktree item points to the new locations, so in this example it should be worktree = ../../../../../new/parent/module
. Typically there should be two more ..
than directories in the direct path in that place.Edit the file new/parent/module/.git
, make sure that the path in it points to the correct new location inside the main project .git
folder, so in this example gitdir: ../../../.git/modules/new/parent/submodule
.
git status
output looks like this for me afterwards:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: .gitmodules
# renamed: old/parent/submodule -> new/parent/submodule
#
Finally, commit the changes.
The trick seems to be understanding that the .git
directory for submodules are now kept in the master repository, under .git/modules
, and each submodule has a .git
file that points to it. This is the procedure you need now:
.git
file in the submodule's working directory, and modify the path it contains so that it points to the right directory in the master repository's .git/modules
directory..git/modules
directory, and find the directory corresponding to your submodule.config
file, updating the worktree
path so that it points to the new location of the submodule's working directory..gitmodules
file in the root of the master repository, updating the path to the working directory of the submodule.git add -u
git add <parent-of-new-submodule-directory>
(It's important that you add the parent, and not the submodule directory itself.)A few notes:
[submodule "submodule-name"]
lines in .gitmodules
and .git/config
must match each other, but don't correspond to anything else..git
directory must correctly point to each other..gitmodules
and .git/config
files should be synchronised.