thi*_*sch 331 git git-submodules
我想在我的Git超级项目中更改Git子模块的目录名.
假设我的.gitmodules
文件中有以下条目:
[submodule ".emacs.d/vimpulse"]
path = .emacs.d/vimpulse
url = git://gitorious.org/vimpulse/vimpulse.git
Run Code Online (Sandbox Code Playgroud)
我必须键入什么来移动.emacs.d/vimpulse
目录.emacs.d/vendor/vimpulse
而不首先删除它(在这里和这里解释
),然后重新添加它.
Git真的需要子模块标签中的整个路径
[submodule ".emacs.d/vimpulse"]
Run Code Online (Sandbox Code Playgroud)
或者是否也可以只存储子项目的名称?
[submodule "vimpulse"]
Run Code Online (Sandbox Code Playgroud)
Axe*_*ert 339
注意:正如评论中所提到的,这个答案指的是旧版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 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
.通常,..
在该位置的直接路径中应该有两个以上的目录.编辑文件new/parent/module/.git
,确保其中的路径指向主项目.git
文件夹中的正确新位置,因此在此示例中gitdir: ../../../.git/modules/new/parent/submodule
.
git status
之后输出对我来说是这样的:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: .gitmodules
# renamed: old/parent/submodule -> new/parent/submodule
#
Run Code Online (Sandbox Code Playgroud)最后,提交更改.
pha*_*ann 222
最现代的答案来自Valloric上面的评论:
git mv old/submod new/submod
git status
.进行验证.)git commit
你很高兴!完成!
Mat*_*lly 55
在我的例子中,我想将一个子模块从一个目录移动到一个子目录,例如"AFNetworking" - >"ext/AFNetworking".这些是我遵循的步骤:
[core] worktree
行.我变成../../../AFNetworking
了../../../../ext/AFNetworking
gitdir
.我变成../.git/modules/AFNetworking
了../../git/modules/ext/AFNetworking
git add .gitmodules
git rm --cached AFNetworking
git submodule add -f <url> ext/AFNetworking
最后,我在git状态中看到:
matt$ git status
# On branch ios-master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: .gitmodules
# renamed: AFNetworking -> ext/AFNetworking
Run Code Online (Sandbox Code Playgroud)
瞧瞧.上面的例子不会改变目录深度,这会对任务的复杂性产生很大的影响,并且不会改变子模块的名称(这可能不是必需的,但我这样做是为了与如果我在那条路径上添加了一个新模块,就会发生.)
Mar*_*ski 20
[更新:2014-11-26]由于Yar在下面做了很好的总结,在你做任何事情之前,请确保你知道子模块的URL.如果未知,请打开.git/.gitmodules
并检查密钥submodule.<name>.url
.
对我有用的是删除旧的子模块,git submodule 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"
Run Code Online (Sandbox Code Playgroud)
小智 11
诀窍似乎是要理解.git
子模块的目录现在保存在主存储库中.git/modules
,并且每个子模块都有一个.git
指向它的文件.这是您现在需要的程序:
.git
子模块工作目录中的文件,并修改它包含的路径,使其指向主存储库目录中的正确.git/modules
目录..git/modules
目录,找到与子模块对应的目录.config
文件,更新worktree
路径,使其指向子模块工作目录的新位置..gitmodules
主存储库根目录中的文件,更新子模块工作目录的路径.git add -u
git add <parent-of-new-submodule-directory>
(重要的是添加父项,而不是子模块目录本身.)几点说明:
[submodule "submodule-name"]
以行.gitmodules
和.git/config
必须相互匹配,但不符合任何东西..git
目录必须正确指向彼此..gitmodules
和.git/config
文件应该是同步的.您只需添加一个新的子模块,然后使用标准命令删除旧的子模块.(应该防止.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>
Run Code Online (Sandbox Code Playgroud)
检查将'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}"
Run Code Online (Sandbox Code Playgroud)
大子模块的奖励方法:
如果子模块很大并且您不想等待克隆,则可以使用旧的原点创建新的子模块,然后切换原点.
示例(使用相同的示例设置)
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
...
Run Code Online (Sandbox Code Playgroud)
"[子模块"]之后引号中的字符串无关紧要.如果需要,您可以将其更改为"foobar".它用于在".git/config"中查找匹配的条目.
因此,如果您在运行"git submodule init"之前进行了更改,它将正常工作.如果您进行更改(或通过合并获取更改),则需要手动编辑.git/config或再次运行"git submodule init".如果您执行后者,您将留下一个无害的"搁浅"条目,其中包含.git/config中的旧名称.
归档时间: |
|
查看次数: |
89036 次 |
最近记录: |