如何在Git存储库中移动现有的Git子模块? [英] How do I move an existing Git submodule within a Git repository?
问题描述
假设我在中有下列条目: .gitmodules
file:
[submodule.emacs.d / vimpulse]
$ =
必须输入什么信息才能将 .emacs.d / vimpulse
目录移至 .emacs.d / vendor / vimpulse
不先删除它(解释
here 和此处),然后重新添加。
Git是否真的需要子模块标记中的整个路径
[submodule .emacs.d / vimpulse]
还可以只存储子项目的名称?
$ b $ div class =h2_lin>解决方案
注意:正如评论中提到的,这个答案指的是旧版本git所需的步骤。 Git现在支持移动子模块:
由于git 1.8.5,
git mv old / submod new / submod
按预期工作,并为你做所有的管道工作。您可能希望使用git 1.9.3或更新版本,因为它包含修复子模块移动的问题。
与删除子模块的方式类似(请参阅如何删除一个子模块?):
- 编辑
.gitmodules
并更改如果需要的话,创建子目录的父目录,并将其放在索引中,并将其放在git add .gitmodules
。 - 子模块的新位置(
mkdir -p new / parent
)。 - 将所有内容从旧目录移动到新目录(
mv -vi old / parent / submodule new / parent / submodule
)。 - 确保Git跟踪这个目录(
git add new / parent
)。 - 使用
git rm --cached old / parent / submodule
。
- 移动目录
.git / modules / old / parent / submodule
其所有内容.git / modules / new / parent / submodule
。 - 编辑
.git / modules / new / parent / config
文件,确保worktree项指向新的位置,所以在这个例子中它应该是worktree = ../。 ./../../../新/父/模块
。通常应该有两个..
然后在那个地方的直接路径中的目录。 编辑文件new / parent / module / .git
,确保其中的路径指向主项目中的正确新位置.git
文件夹,所以在本例中gitdir:../../../。git / modules / new / parent / submodule
。
git status
输出结果如下:
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...以取消存储)
#
#modified:.gitmodules
#重命名为:old / parent / submodule - >新/父/子模块
#
-
最后,提交更改。 p>
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"]
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.
It’s similar to how you remove a submodule (see How do I remove a submodule?):
- Edit
.gitmodules
and change the path of the submodule appropriately, and put it in the index withgit 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 beworktree = ../../../../../new/parent/module
. Typically there should be two more..
then 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 examplegitdir: ../../../.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.
这篇关于如何在Git存储库中移动现有的Git子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!