Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 here and here) and then re-adding 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"] 
like image 274
thisch Avatar asked Jan 05 '11 13:01

thisch


People also ask

How do I move a submodule?

Git Submodules Moving a submodule If needed, create the parent directory of the new location of the submodule ( mkdir -p new/path/to ). Move all content from the old to the new directory ( mv -vi old/path/to/module new/path/to/submodule ). Make sure Git tracks this directory ( git add new/path/to ).

How do I add a submodule to an existing repo?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

How do I clone a submodules repo?

How do I clone a git repository so that it also clones its submodules? Running git clone $REPO_URL merely creates empty submodule directories. That command would be git clone --recurse-submodules --remote-submodules (Q3 2019 Git 2.23): it will clone and update the submodules in one command.

Can you push to a git submodule?

Make sure all submodule commits used by the revisions to be pushed are available on a remote-tracking branch. If the value is ' check ', then Git will verify that all submodule commits that changed in the revisions to be pushed are available on at least one remote of the submodule.


1 Answers

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?):

  1. Edit .gitmodules and change the path of the submodule appropriately, and put it in the index with git add .gitmodules.
  2. If needed, create the parent directory of the new location of the submodule (mkdir -p new/parent).
  3. Move all content from the old to the new directory (mv -vi old/parent/submodule new/parent/submodule).
  4. Make sure Git tracks this directory (git add new/parent).
  5. Remove the old directory with git rm --cached old/parent/submodule.
  6. Move the directory .git/modules/old/parent/submodule with all its content to .git/modules/new/parent/submodule.
  7. Edit the .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.
  8. 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 # 
  9. Finally, commit the changes.

like image 116
Axel Beckert Avatar answered Sep 30 '22 10:09

Axel Beckert