重命名 git 子模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4526910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename a git submodule
提问by Lars Tackmann
Is there some easy way to rename a git submodule directory (other than going through the entire motion of deletingit and re-adding it with a new destination name).
是否有一些简单的方法来重命名 git 子模块目录(除了完成删除它并使用新的目标名称重新添加它的整个动作 )。
And while we are at it, why is it that I simply cannot do the following in the parent directory: git mv old-submodule-name new-submodule-name
当我们这样做时,为什么我根本无法在父目录中执行以下操作: git mv old-submodule-name new-submodule-name
采纳答案by Mariusz Nowak
I found following workflow working:
我发现以下工作流程有效:
- Update .gitmodules
mv oldpath newpath
git rm oldpath
git add newpath
git submodule sync
- 更新 .gitmodules
mv oldpath newpath
git rm oldpath
git add newpath
git submodule sync
Note: this approach does not update the index and .gitmodules properly in 2018 versions of GIT.
注意:这种方法不会在 2018 版本的 GIT 中正确更新索引和 .gitmodules。
Note: You may be able to now just do git mv oldpath newpath
now, as pointed out in VonC's answer. (Ensure you are using the latest version of git)
注意:git mv oldpath newpath
正如VonC的回答中指出的那样,您现在可以立即执行。(确保您使用的是最新版本的 git)
回答by VonC
Git1.8.5 (October 2013) should simplify the process. Simply do a:
Git1.8.5(2013 年 10 月)应该简化这个过程。只需做一个:
git mv A B
"
git mv A B
", when moving a submoduleA
has been taught to relocate its working tree and to adjust the paths in the.gitmodules
file.
“
git mv A B
”,当移动一个子模块时A
,会重新定位它的工作树并调整.gitmodules
文件中的路径。
See more in commit 0656781fadca1:
在提交 0656781fadca1 中查看更多信息:
Currently using "
git mv
" on a submodule moves the submodule's work tree in that of the superproject. But the submodule's path setting in.gitmodules
is left untouched, which is now inconsistent with the work tree and makes git commands that rely on the properpath -> name mapping
(likestatus
anddiff
) behave strangely.Let "
git mv
" help here by not only moving the submodule's work tree but also updating the "submodule.<submodule name>.path
" setting from the.gitmodules
file and stage both.
This doesn't happen when no.gitmodules
file is found and only issues a warning when it doesn't have a section for this submodule. This is because the user might just use plain gitlinks without the.gitmodules
file or has already updated the path setting by hand before issuing the "git mv" command (in which case the warning reminds him thatmv
would have done that for him).
Only when.gitmodules
is found and contains merge conflicts themv
command will fail and tell the user to resolve the conflict before trying again.
当前
git mv
在子模块上使用“ ”会将子模块的工作树移动到超级项目的工作树中。但是子模块的路径设置.gitmodules
保持不变,这现在与工作树不一致,并使依赖于正确path -> name mapping
(如status
和diff
)的git 命令表现得很奇怪。让 "
git mv
" 在这里提供帮助,不仅可以移动子模块的工作树,还可以更新文件和暂存中的 "submodule.<submodule name>.path
" 设置.gitmodules
。
当没有.gitmodules
找到文件时不会发生这种情况,并且只有在它没有此子模块的部分时才会发出警告。这是因为用户可能只使用没有.gitmodules
文件的普通 gitlinks,或者在发出“git mv”命令之前已经手动更新了路径设置(在这种情况下,警告提醒他mv
本来可以为他做的)。
只有当.gitmodules
找到并包含合并冲突时,该mv
命令才会失败并告诉用户在重试之前解决冲突。
git 2.9 (June 2016) will improve git mv
for submodule:
git 2.9(2016 年 6 月)将改进git mv
子模块:
See commit a127331(19 Apr 2016) by Stefan Beller (stefanbeller
).
(Merged by Junio C Hamano -- gitster
--in commit 9cb50a3, 29 Apr 2016)
请参阅Stefan Beller ( ) 的提交 a127331(2016 年 4 月 19 日)。(由Junio C Hamano合并-- --在提交 9cb50a3,2016 年 4 月 29 日)stefanbeller
gitster
mv
: allow moving nested submodules"
git mv old new
" did not adjust the path for a submodule that lives as a subdirectory insideold/
directory correctly.submodules however need to update their link to the git directory as well as updates to the
.gitmodules
file.
mv
: 允许移动嵌套的子模块"
git mv old new
" 没有正确调整作为目录内子目录存在的子模块的路径old/
。然而,子模块需要更新它们到 git 目录的链接以及对
.gitmodules
文件的更新。
回答by Mahmoud Adam
The below solution didn't work for me:
以下解决方案对我不起作用:
mv oldpath newpath
git rm oldpath
git add newpath
git submodule sync
Because when using git add
command, the submodule was included in the project as a simple directory and not as a submodule.
因为在使用git add
command 时,子模块作为一个简单的目录包含在项目中,而不是作为子模块。
The correct solution is:
正确的解决办法是:
mv oldpath ~/another-location
git rm oldpath
git submodule add submodule-repository-URL newpath
Source: Rename git submodule
来源:重命名 git 子模块
回答by jaredwolff
I just tried a few of the suggested above. I'm running:
我只是尝试了上面建议的一些。我在跑:
$ git --version
git version 1.8.4
I found it was best to de-init the submodule, remove the directory and create a new submodule.
我发现最好取消初始化子模块,删除目录并创建一个新的子模块。
git submodule deinit <submodule name>
git rm <submodule folder name>
git submodule add <address to remote git repo> <new folder name>
At least that is what worked for me best. YMMV!
至少这对我来说是最有效的。天啊!
回答by kenorb
It's not possible to rename it, so you've to remove it first (deinit
) and add it again.
无法对其进行重命名,因此您必须先将其删除 ( deinit
) 并重新添加。
So after removing it:
所以删除后:
git submodule deinit <path>
git rm --cached <path>
you may also double check and remove the references to it in:
您还可以仔细检查并删除对它的引用:
.gitmodules
.git/config
- remove reference folder from
.git/modules/<name>
(best to make a backup), as each folder hasconfig
file where it keeps the reference to itsworktree
.gitmodules
.git/config
- 从
.git/modules/<name>
(最好进行备份)中删除引用文件夹,因为每个文件夹都有一个config
文件,它保留对其的引用worktree
then stage your changes by committing any changes to your repo by:
然后通过将任何更改提交到您的存储库来暂存您的更改:
git commit -am 'Removing submodule.'
and double check if you don't have any outstanding issues by:
并通过以下方式仔细检查您是否没有任何未解决的问题:
git submodule update
git submodule sync
git submodule status
so now you can add the git submodule again:
所以现在你可以再次添加 git 子模块:
git submodule add --name <custom_name> [email protected]:foo/bar.git <my/path>
回答by Abizern
Edit the .gitmodules file to rename the submodule and then rename the submodule directory.
编辑 .gitmodules 文件以重命名子模块,然后重命名子模块目录。
I think you might need to do a git submodule sync
afterwards, but I'm not in a position to check right now.
我想你可能需要git submodule sync
事后做一个,但我现在无法检查。
回答by Kamil Kie?czewski
MacOs: When I wanna use VonC solutionto change submodule folder Common
to lowercase:
MacOs:当我想使用VonC 解决方案将子模块文件夹更改Common
为小写时:
git mv Common common
I get
我得到
fatal: renaming 'Common' failed: Invalid argument
致命:重命名“Common”失败:参数无效
Solution - use some temporary folder name and move twice:
解决方案 - 使用一些临时文件夹名称并移动两次:
git mv Common commontemp
git mv commontemp common
That's all :)
就这样 :)