[英]Changing the remote repository of git submodule for all commits in git history
How can I change the remote url of a git submodule without breaking commit history?如何在不破坏提交历史记录的情况下更改 git 子模块的远程 url? Currently if I checkout old commits my submodule seems to have the old remote url witch doesn't exist anymore.目前,如果我检查旧提交,我的子模块似乎不再存在旧的远程 url 女巫。
My workflow:我的工作流程:
Push.gitmodules to remote (Commit C) Push.gitmodules 到远程(提交 C)
Clone repo with commit A,B,C使用提交 A、B、C 克隆存储库
-> want also the new submodule remote URL, but still the old url in.gitmodules -> 还想要新的子模块远程 URL,但仍然是旧的 url in.gitmodules
It seems that git submodule update --init
is still trying to use the old remote url看来git submodule update --init
仍在尝试使用旧的远程 url
If have also tried to use git submodule update
, as suggested, but I also got the error:如果还按照建议尝试使用git submodule update
,但我也得到了错误:
The Git repository with name or identifier OLD_REPO_NAME does not exist or you do not have permissions for the operation you are attempting.
Let's try another approach.让我们尝试另一种方法。 Instead of replacing URLs in the config files replace URLs on the fly:而不是替换配置文件中的 URL,而是动态替换 URL:
git config --global url.<NEW-URL>.insteadOf <OLD-URL>
See git help config
about url.<base>.insteadOf
.请参阅git help config
url.<base>.insteadOf
。
Commits are not tied to remotes.提交与远程无关。 Once you changed a remote the change is in effect for all commits, old and new.一旦您更改了遥控器,该更改对所有新旧提交都有效。
To change the URL for a submodule you need:要为您需要的子模块更改 URL:
.gitmodules
in the root of the superproject;更改超级项目根目录下 .gitmodules 文件中的.gitmodules
;git submodule sync
to copy the change to .git/config
运行git submodule sync
将更改复制到.git/config
cd
into the submodule and run git remote set-url
to change the URL in the submodule. cd
进入子模块并运行git remote set-url
以更改子模块中的 URL。 When you checkout an old commit in the superproject you get old .gitmodules
but that doesn't matter — git
doesn't use URLs from .gitmodules
— it uses URLs from .git/config
.当您在超级项目中签出旧提交时,您会得到旧的.gitmodules
但这没关系git
不使用来自 .gitmodules 的.gitmodules
它使用来自.git/config
的 URL。 git sync
copies the URLs from .gitmodules
to .git/config
. git sync
将 URL 从.gitmodules
复制到.git/config
。 So after checking out an old commit with an old .gitmodules
DO NOT run git sync
and git
will continue using new URLs from .git/config
.因此,在使用旧的.gitmodules
签出旧提交后,请勿运行git sync
,并且git
将继续使用来自.git/config
的新 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.