重命名git子模块

Lar*_*ann 137 git git-submodules

是否有一些简单的方法来重命名git子模块目录(除了完成删除它并重新添加新目标名称的整个动作 ).

虽然我们在这,但为什么我不能在父目录中执行以下操作: git mv old-submodule-name new-submodule-name

Von*_*onC 200

Git1.8.5(2013年10月)应简化流程.只需做一个:

git mv A B
Run Code Online (Sandbox Code Playgroud)

" git mv A B",当移动子模块时A,已经教会重新定位其工作树并调整.gitmodules文件中的路径.


提交0656781fadca1中查看更多:

目前git mv在子模块上使用" "会将子模块的工作树移动到超级项目的工作树中.但子模块的路径设置.gitmodules保持不变,现在与工作树不一致,并使git命令依赖于正确的path -> name mapping(如statusdiff)行为奇怪.

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 that mv would have done that for him).
Only when .gitmodules is found and contains merge conflicts the mv command will fail and tell the user to resolve the conflict before trying again.


git 2.9 (June 2016) will improve git mv for submodule:

请参阅Stefan Beller()提交a127331(2016年4月19日).(由Junio C Hamano合并- -提交9cb50a3,2016年4月29日)stefanbeller
gitster

mv:允许移动嵌套的子模块

" git mv old new"没有正确调整作为目录内部子目录的子模块的路径old/.

然而,子模块需要更新它们到git目录的链接以及对.gitmodules文件的更新.

  • 这应该是今天"接受"的解决方案.:/ (8认同)
  • @MaksimDmitriev但是我在http://stackoverflow.com/a/20918469/6309中提到的ppa(个人包存档)怎么样? (2认同)
  • git mv 在我的机器上不适用于此目的。 (2认同)

Mar*_*wak 89

我发现以下工作流程工作:

  • 更新.gitmodules
  • mv oldpath newpath
  • git rm oldpath
  • git add newpath
  • git submodule sync

注意:此方法不会在2018版本的GIT中正确更新索引和.gitmodules.

  • 自我注意:对于`git add newpath`不要使用尾部斜杠 (22认同)
  • 使用这样的工作流程,您将只为您用作子模块的子目录创建符号链接.Symlink只是全局子模块功能的一部分.这意味着当您克隆使用重命名的子模块的repo时,可能会出现以下错误:*.gitmodules中没有找到路径'your-oldpath'的子模块映射删除oldpath(*git rm oldpath*)后,你应该使用*git submodule添加REPO-URL newpath*而不是"git add newpath*.完成后,git status将显示如下内容:renamed:oldpath - > newpath (10认同)
  • 这个解决方案对我不起作用,因为在使用git add命令时,子模块作为简单目录而不是子模块包含在项目中.尝试$ mv submodule-oldpath~/another-location $ git rm submodule-oldpath $ git submodule add submodule-repository-URL submodule-newpath http://bcachet.github.io/development/2012/05/25/rename-git -submodule / (5认同)
  • 如果你确实遇到了工作树的问题,你也可以编辑`.git/modules/SUBMODULE/config`文件,这样``worktree`再次指向正确的目录. (3认同)
  • 这对git`2.13.1`无效。但是简单地执行git mv old new就像一个魅力。我只需要执行git commit即可完成更改。 (3认同)

Mah*_*dam 9

$ mv submodule-oldpath submodule-newpath
$ git rm submodule-oldpath
$ git add submodule-newpath
$ git submodule sync
Run Code Online (Sandbox Code Playgroud)

这个解决方案对我不起作用,因为在使用git add命令时,子模块作为简单目录而不是子模块包含在项目中.

正确的解决方案是:

$ mv submodule-oldpath ~/another-location
$ git rm submodule-oldpath
$ git submodule add submodule-repository-URL submodule-newpath
Run Code Online (Sandbox Code Playgroud)

资料来源:http://bcachet.github.io/development/2012/05/25/rename-git-submodule/

  • 你是从这个地方一字不漏地说出来的吗?http://bcachet.github.io/development/2012/05/25/rename-git-submodule/如果是这样,请务必在答案中提供该网站的归属信息. (2认同)

jar*_*lff 9

我刚刚尝试了上面提到的一些建议.我在跑:

$ git --version
git version 1.8.4
Run Code Online (Sandbox Code Playgroud)

我发现最好取消初始化子模块,删除目录并创建一个新的子模块.

git submodule deinit <submodule name>

git rm <submodule folder name>

git submodule add <address to remote git repo> <new folder name>
Run Code Online (Sandbox Code Playgroud)

至少这对我最有用.因人而异!


ken*_*orb 9

无法对其进行重命名,因此您必须先将其删除 ( deinit) 并重新添加。

所以删除后:

git submodule deinit <path>
git rm --cached <path>
Run Code Online (Sandbox Code Playgroud)

您还可以仔细检查并删除对它的引用:

  • .gitmodules
  • .git/config
  • .git/modules/<name>(最好进行备份)中删除引用文件夹,因为每个文件夹都有一个config文件,它保留对其的引用worktree

然后通过将任何更改提交到您的存储库来暂存您的更改:

git commit -am 'Removing submodule.'
Run Code Online (Sandbox Code Playgroud)

并通过以下方式仔细检查您是否没有任何未解决的问题:

git submodule update
git submodule sync
git submodule status
Run Code Online (Sandbox Code Playgroud)

所以现在你可以再次添加 git 子模块:

git submodule add --name <custom_name> git@github.com:foo/bar.git <my/path>
Run Code Online (Sandbox Code Playgroud)

  • 问题标题用词不当,它涉及路径,而不是名称 (3认同)

Kam*_*ski 5

MacOs:当我想使用VonC 解决方案将子模块文件夹更改Common为小写时:

git mv Common common
Run Code Online (Sandbox Code Playgroud)

我明白了

致命:重命名“Common”失败:参数无效

解决方案 - 使用一些临时文件夹名称并移动两次:

git mv Common commontemp
git mv commontemp common
Run Code Online (Sandbox Code Playgroud)

就这样 :)