How do I correctly move a git submodule?

How do I move an existing git submodule within a git repository?

  • 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: [submodule ".emacs.d/vimpulse"] path = .emacs.d/vimpulse url = git://gitorious.org/vimpulse/vimpulse.git What do I have to type to move the .emacs.d/vimpulse directory to .emacs.d/vendor/vimpulse without deleting it first (explained http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule and https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial) and then readding it. Does git really need the whole path in the submodule tag [submodule ".emacs.d/vimpulse"] or is it also possible to store just the name of the subproject ? [submodule "vimpulse"] edit: There exists a GSoC11 idea which tries to address the "move submodule"-problem: see https://git.wiki.kernel.org/articles/s/o/c/SoC2011Ideas_49fd.html and http://wiki.github.com/jlehmann/git-submod-enhancements

  • Answer:

    It's similar to how you remove a submodule (see http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule): Edit .gitmodules and change the path of the submodule appropriately, and put it in the index with "git add .gitmodules". If needed, create the parent directory of the new location of the submodule: "mkdir -p new/parent" and make sure git tracks this directory ("git add new/parent") Move all content from the old to the new directory: "mv -vi old/parent/submodule new/parent/submodule" Remove the old directory with "git rm --cached old/parent/submodule". 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.

thisch at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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.

Bob Bell

The given solution did not work for me, however a similar version did... This is with a cloned repository, hence the submodule git repos are contained in the top repositories .git dir. All cations are from the top repository: Edit .gitmodules and change the "path =" setting for the submodule in question. (No need to change the label, nor to add this file to index.) Edit .git/modules/name/config and change the "worktree =" setting for the submodule in question run: mv submodule newpath/submodule git add -u git add newpath/submodule I wonder if it makes a difference if the repositories are atomic, or relative submodules, in my case it was relative (submodule/.git is a ref back to topproject/.git/modules/submodule)

arand

In my case, I wanted to move a submodule from one directory into a subdirectory, e.g. "AFNetworking" -> "ext/AFNetworking". These are the steps I followed: Edit .gitmodules changing submodule name and path to be "ext/AFNetworking" Move submodule's git directory from ".git/modules/AFNetworking" to ".git/modules/ext/AFNetworking" Move library from "AFNetworking" to "ext/AFNetworking" Edit ".git/modules/ext/AFNetworking/config" and fix the [core] worktree line. Mine changed from ../../../AFNetworking to ../../../../ext/AFNetworking Edit "ext/AFNetworking/.git" and fix gitdir. Mine changed from ../.git/modules/AFNetworking to ../../git/modules/ext/AFNetworking git add .gitmodules git rm --cached AFNetworking git submodule add -f <url> ext/AFNetworking Finally, I saw in the git status: matt$ git status # On branch ios-master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: .gitmodules # renamed: AFNetworking -> ext/AFNetworking Et voila. The above example doesn't change the directory depth, which makes a big difference to the complexity of the task, and doesn't change the name of the submodule (which may not really be necessary, but I did it to be consistent with what would happen if I added a new module at that path.)

Matt Connolly

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.