我想在我的Git超级项目中更改一个Git子模块的目录名称。

让我们假设我的.gitmodules文件中有以下条目:

[submodule ".emacs.d/vimpulse"]  
path = .emacs.d/vimpulse  
url = git://gitorious.org/vimpulse/vimpulse.git

我必须输入什么来移动。emacs。D /vimpulse目录到。emacs。D /vendor/vimpulse而不首先删除它(解释 这里和这里),然后重新添加。

Git真的需要子模块标签中的整个路径吗

[submodule ".emacs.d/vimpulse"]

或者也可以只存储子项目的名称?

[submodule "vimpulse"]

当前回答

诀窍似乎是理解子模块的.git目录现在保存在主存储库中,在.git/modules下,每个子模块都有一个指向它的.git文件。这是你现在需要的程序:

Move the submodule to its new home. Edit the .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. Enter the master repository's .git/modules directory, and find the directory corresponding to your submodule. Edit the config file, updating the worktree path so that it points to the new location of the submodule's working directory. Edit the .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.)

注意事项:

.gitmodules和.git/config中的[submodule "submodule-name"]行必须相互匹配,但不对应任何其他内容。 子模块的工作目录和.git目录必须正确指向对方。 .gitmodules和.git/config文件应该同步。

其他回答

只需使用shell脚本git-submodule-move。

“[submodule”后面引号中的字符串并不重要。如果你愿意,你可以把它改成“foobar”。它用于在“.git/config”中查找匹配的条目。

因此,如果你在运行"git submodule init"之前进行更改,它会正常工作。如果你做了更改(或通过合并进行更改),你需要手动编辑.git/config或再次运行"git submodule init"。如果您选择后者,您将在.git/config中留下一个带有旧名称的无害的“搁浅”条目。

更新版本的git

Git现在支持移动子模块:

从git 1.8.5开始,git mv old/submod new/submod就像预期的那样工作,并为你做了所有的管道工作。您可能希望使用git 1.9.3或更新版本,因为它包含了针对子模块移动的修复。

git的旧版本

正如评论中提到的,这个答案指的是旧版本git所需的步骤。

这个过程类似于如何删除子模块(参见如何删除子模块?):

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). Move all content from the old to the new directory (mv -vi old/parent/submodule new/parent/submodule). Make sure Git tracks this directory (git add new/parent). Remove the old directory with git rm --cached old/parent/submodule. Move the directory .git/modules/old/parent/submodule with all its content to .git/modules/new/parent/submodule. 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. 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.

[更新:2014-11-26]正如Yar在下面总结的那样,在你做任何事情之前,确保你知道子模块的URL。如果未知,打开.git/。<name>.url。

对我有用的是使用git子模块deinit <submodule>和git rm <submodule-folder>删除旧的子模块。然后用新的文件夹名再次添加子模块并提交。在提交前检查git状态会显示旧子模块重命名为新名称,.gitmodule被修改。

$ 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"

诀窍似乎是理解子模块的.git目录现在保存在主存储库中,在.git/modules下,每个子模块都有一个指向它的.git文件。这是你现在需要的程序:

Move the submodule to its new home. Edit the .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. Enter the master repository's .git/modules directory, and find the directory corresponding to your submodule. Edit the config file, updating the worktree path so that it points to the new location of the submodule's working directory. Edit the .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.)

注意事项:

.gitmodules和.git/config中的[submodule "submodule-name"]行必须相互匹配,但不对应任何其他内容。 子模块的工作目录和.git目录必须正确指向对方。 .gitmodules和.git/config文件应该同步。