简体   繁体   English

如何使用子模块重命名git存储库?

[英]How can I rename a git repository with submodules?

I have a git repository with submodules in the directory projects/myRepo and I want to rename the directory to projects/my-repo . 我在目录projects/myRepo有一个projects/myRepo模块的git存储库,我想将目录重命名为projects/my-repo

According to this question it can simply be done with mv . 根据这个问题,它可以简单地用mv完成。 But in a repo with submodules git keeps telling me 但在子模块的回购中,git一直在告诉我

fatal: Not a git repository: projects/myRepo/.git/path/to/submodule``` 致命的:不是git存储库:projects / myRepo / .git / path / to / submodule```

even for git status . 即使是git status

Submodule config: 子模块配置:

[submodule "path/to/submodule"]  
   path = path/to/submodule  
   url = https://github.com/user/projectName.git  

Somehow the 'internal path' for the submodule does not get updated?! 不知何故,子模块的“内部路径”没有更新?! Is there a way to tell git to update these submodule paths? 有没有办法告诉git更新这些子模块路径?

I also had the same error after changing my project directory. 更改项目目录后,我也遇到了同样的错误。 I have an iOS 6 XCode 4 project but that shouldn't matter. 我有一个iOS 6 XCode 4项目,但这无关紧要。

For each submodule, you need to change the absolute path that it thinks it's in. The path is set in the .git file for that submodule. 对于每个子模块,您需要更改它认为它所在的绝对路径。路径在该子模块的.git文件中设置。 .git in a submodule is a file as opposed to a directory in a standard git directory. git的一个子模块,而不是一个目录在标准的git目录中的文件

For each submodule, change the .gitdir line in the .git file. 对于每个子模块,更改.git文件中的.gitdir行。 Here is an example from my project: 这是我项目的一个例子:

File: /path/to/project/submodules/RestKit/.git 文件:/path/to/project/submodules/RestKit/.git

Before gitdir: /path/to/project//.git/modules/submodules/RestKit gitdir 之前 :/path/to/project//.git/modules/submodules/RestKit

After gitdir: /path/to/project//.git/modules/submodules/RestKit gitdir 之后 :/path/to/project//.git/modules/submodules/RestKit

I did experience exactly the same behavior. 我确实遇到了完全相同的行为。 I managed to fix it by deleting directories with submodules, recreating them as empty directories with the right name and then running git submodule update --init to reinitialize them. 我设法通过删除带有子模块的目录来修复它,将它们重新创建为具有正确名称的空目录,然后运行git submodule update --init以重新初始化它们。 All fixed now. 全部修复了。 Probably some permission problems (I retrieved those directories from backup earlier and permissions on them are sometimes strange). 可能是一些权限问题(我之前从备份中检索了这些目录,对它们的权限有时很奇怪)。

Today I had the same problem to rename the submodule and finally I fixed it by using the following steps: 今天我有同样的问题重命名子模块,最后我通过使用以下步骤修复它:

Assume the old module name is old/module and the new one is new/module/path 假设旧模块名称为old/module ,新模块名称为new/module/path

  1. at the root of the repo ( repo_root ), mv old/module new/module/path 在repo的根目录( repo_root ), mv old/module new/module/path
  2. go to .git/modules 转到.git/modules
    1. mv old/module new/module/path (create the folder first if necessary) mv old/module new/module/path (必要时先创建文件夹)
    2. change new/module/name/config : update the worktree entry. 更改new/module/name/config :更新worktree条目。 It should be the relative path from this config file to repo_root/new/module/path 它应该是从此配置文件到repo_root/new/module/path的相对repo_root/new/module/path
  3. go to repo_root and edit new/module/path/.git , change the gitdir to the relative path from this file to repo_root/.git/modules/new/module/path 转到repo_root并编辑new/module/path/.git ,将gitdir更改为gitdir文件到repo_root/.git/modules/new/module/path
  4. change .git/config of the master repo: find the line containing [submodule "old/module"] and update to [submodule "new/module/path"] 更改主.git/config :找到包含[submodule "old/module"]并更新到[submodule "new/module/path"]

Since Weston 's test (clone, init and update of a submodule in a cloned repo) worked as expected when renaming the directory including said submodule, I suspect something must have been wrong in the submodule .git itself in your original Git repo submodule. 由于Weston的测试(克隆,初始化和更新克隆仓库中的子模块)在重命名包含所述子模块的目录时按预期工作,我怀疑在原始Git repo子模块.git模块.git本身一定有问题。

You could compare/look for any file referencing ' myRepo ' in that first submodule. 您可以在第一myRepo模块中比较/查找引用“ myRepo ”的任何文件。
you can also compare the content of the two submodules (the first one you tried to rename, and the second one included in the repo that you cloned) 您还可以比较两个子模块的内容(您尝试重命名的第一个子模块,以及您克隆的存储库中包含的第二个子模块)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM