我想在我的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 mv <old/path> <new/path失败:

Rename from 'old/path' to 'new/path' failed. Should I try again? (y/n)

在运行git deinit <old/path>后,我能够使用git mv。

其他回答

我遇到同样的问题并成功地解决了它。感谢您在Github发布delete_git_submodule.md

要移除一个子模块,你需要:

从.gitmodules文件中删除相关的部分。 阶段。gitmodules的变化,git添加。gitmodules 从.git/config中删除相关的部分。(也许并不存在) 运行git rm——cached path_to_submodule(没有后面的斜杠)。 执行rm -rf .git/modules/path_to_submodule(没有后面的斜杠)。 Commit -m "已删除的子模块" 删除现在不跟踪的子模块文件rm -rf path_to_submodule

您只需添加一个新的子模块,并使用标准命令删除旧的子模块。(应该防止.git内部的任何意外错误)

示例设置:

mkdir foo; cd foo; git init; 
echo "readme" > README.md; git add README.md; git commit -m "First"
## add submodule
git submodule add git://github.com/jquery/jquery.git
git commit -m "Added jquery"
## </setup example>

移动jquery到vendor/jquery/jquery的例子:

oldPath="jquery"
newPath="vendor/jquery/jquery"
orginUrl=`git config --local --get submodule.${oldPath}.url`

## add new submodule
mkdir -p `dirname "${newPath}"`
git submodule add -- "${orginUrl}" "${newPath}"

## remove old submodule
git config -f .git/config --remove-section "submodule.${oldPath}"
git config -f .gitmodules --remove-section "submodule.${oldPath}"
git rm --cached "${oldPath}"
rm -rf "${oldPath}"              ## remove old src
rm -rf ".git/modules/${oldPath}" ## cleanup gitdir (housekeeping)

## commit
git add .gitmodules
git commit -m "Renamed ${oldPath} to ${newPath}"

大子模块的奖励方法:

如果子模块很大,并且您不想等待克隆,那么可以使用旧的子模块作为源来创建新的子模块,然后切换源。

示例(使用相同的示例设置)

oldPath="jquery"
newPath="vendor/jquery/jquery"
baseDir=`pwd`
orginUrl=`git config --local --get submodule.${oldPath}.url`

# add new submodule using old submodule as origin
mkdir -p `dirname "${newPath}"`
git submodule add -- "file://${baseDir}/${oldPath}" "${newPath}"

## change origin back to original
git config -f .gitmodules submodule."${newPath}".url "${orginUrl}"
git submodule sync -- "${newPath}"

## remove old submodule
...

[更新: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 repos包含在顶级存储库.git目录中。所有阳离子都来自顶部存储库:

编辑.gitmodules并更改相关子模块的"path ="设置。(不需要更改标签,也不需要将该文件添加到索引。) 编辑.git/modules/name/config,修改子模块的"worktree ="设置 运行: Mv submodule newpath/submodule Git add -u Git增加了新路径/子模块

我想知道如果存储库是原子的,还是相对的子模块,在我的情况下,它是相对的(submodule/。Git是一个指向topproject/.git/modules/submodule的引用)

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