如何更改 git 子模块的远程存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/913701/
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
How to change the remote repository for a git submodule?
提问by Andrew Grimm
I've created a git repository with a submodule in it. I'm able to tell the submodule itself to change its remote repository path, but I'm not sure how to tell the parent repository how to change the remote repository path for the submodule.
我创建了一个带有子模块的 git 存储库。我可以告诉子模块本身更改其远程存储库路径,但我不确定如何告诉父存储库如何更改子模块的远程存储库路径。
I wouldn't be surprised if I'm somewhat out of luck and have to do things manually, as even deleting submodules isn't easy.
如果我有点不走运并且必须手动执行操作,我不会感到惊讶,因为即使删除子模块也不容易。
回答by Jim Puls
You should just be able to edit the .gitmodules
file to update the URL and then run git submodule sync
to reflect that change to the superproject and your working copy.
您应该能够编辑.gitmodules
文件以更新 URL,然后运行git submodule sync
以将该更改反映到超级项目和您的工作副本。
回答by Pavan Sokke Nagaraj
These commands will do the work on command prompt without altering any files on local repository
这些命令将在命令提示符下完成工作,而不会更改本地存储库中的任何文件
git config --file=.gitmodules submodule.Submod.url https://github.com/username/ABC.git
git config --file=.gitmodules submodule.Submod.branch Development
git submodule sync
git submodule update --init --recursive --remote
Please look at the blog for screenshots: Changing GIT submodules URL/Branch to other URL/branch of same repository
回答by Matthew Wilcoxson
In simple terms, you just need to edit the .gitmodules file, then resync and update:
简单来说,你只需要编辑 .gitmodules 文件,然后重新同步和更新:
Edit the file, either via a git command or directly:
通过 git 命令或直接编辑文件:
git config --file=.gitmodules -e
or just:
要不就:
vim .gitmodules
then resync and update:
然后重新同步并更新:
git submodule sync
git submodule update --init --recursive --remote
回答by VonC
With Git 2.25 (Q1 2020), you can modifyit.
See "Git submodule url changed" and the new command
使用 Git 2.25(2020 年第一季度),您可以对其进行修改。
参见“ Git submodule url changed”和新命令
git submodule set-url [--] <path> <newurl>
Original answer (May 2009, ten years ago)
原始答案(2009 年 5 月,十年前)
Actually, a patch has been submitted in April 2009to clarify gitmodule
role.
实际上,2009 年 4 月已经提交了一个补丁来阐明gitmodule
角色。
So now the gitmodule documentationdoes not yet include:
所以现在gitmodule 文档还没有包括:
The
.gitmodules
file, located in the top-level directory of a git working tree, is a text file with a syntax matching the requirements -of linkgit:git-config1.
[NEW]:
As this file is managed by Git, it tracks the +records of a project's submodules.
Information stored in this file is used as a hint to prime the authoritative version of the record stored in the project configuration file.
User specific record changes (e.g. to account for differences in submodule URLs due to networking situations) should be made to the configuration file, while record changes to be propagated (e.g. +due to a relocation of the submodule source) should be made to this file.
该
.gitmodules
文件位于 git 工作树的顶级目录中,是一个文本文件,其语法符合 -of linkgit:git-config 1 的要求。
[新]:
由于此文件由 Git 管理,因此它会跟踪项目子模块的 + 记录。
此文件中存储的信息用作提示,以准备存储在项目配置文件中的记录的权威版本。
应该对配置文件进行用户特定的记录更改(例如,考虑到由于网络情况而导致子模块 URL 的差异),而要传播的记录更改(例如,由于子模块源的重新定位)应该对这个文件进行.
That pretty much confirm Jim's answer.
这几乎证实了吉姆的回答。
If you follow this git submodule tutorial, you see you need a "git submodule init
" to add the submodule repository URLs to .git/config.
如果你按照这个git 子模块教程,你会发现你需要一个“ git submodule init
”来将子模块存储库 URL 添加到 .git/config。
"git submodule sync
" has been added in August 2008precisely to make that task easier when URL changes (especially if the number of submodules is important).
The associate script with that command is straightforward enough:
“ git submodule sync
”是在 2008 年 8 月添加的,正是为了在 URL 更改时使该任务更容易(尤其是在子模块的数量很重要的情况下)。
带有该命令的关联脚本非常简单:
module_list "$@" |
while read mode sha1 stage path
do
name=$(module_name "$path")
url=$(git config -f .gitmodules --get submodule."$name".url)
if test -e "$path"/.git
then
(
unset GIT_DIR
cd "$path"
remote=$(get_default_remote)
say "Synchronizing submodule url for '$name'"
git config remote."$remote".url "$url"
)
fi
done
The goal remains: git config remote."$remote".url "$url"
目标仍然是: git config remote."$remote".url "$url"
回答by Ben Hymers
What worked for me (on Windows, using git version 1.8.3.msysgit.0):
什么对我有用(在 Windows 上,使用 git 版本 1.8.3.msysgit.0):
- Update .gitmodules with the path to the new repository
- Remove the corresponding line from the ".git/config" file
- Delete the corresponding directory in the ".git/modules/external" directory
- Delete the checked out submodule directory itself (unsure if this is necessary)
- Run
git submodule init
andgit submodule update
- Make sure the checked out submodule is at the correct commit, and commit that, since it's likely that the hash will be different
- 使用新存储库的路径更新 .gitmodules
- 从“.git/config”文件中删除相应的行
- 删除“.git/modules/external”目录下对应的目录
- 删除检出的子模块目录本身(不确定是否有必要)
- 运行
git submodule init
和git submodule update
- 确保检出的子模块在正确的提交中,并提交,因为散列很可能会不同
After doing all that, everything is in the state I would expect. I imagine other users of the repository will have similar pain when they come to update though - it would be wise to explain these steps in your commit message!
完成所有这些之后,一切都处于我期望的状态。我想存储库的其他用户在更新时也会有类似的痛苦 - 在提交消息中解释这些步骤是明智的!
回答by FelipeC
Just edit your .git/configfile. For example; if you have a "common"submodule you can do this in the super-module:
只需编辑您的.git/config文件。例如; 如果您有一个“通用”子模块,您可以在超级模块中执行此操作:
git config submodule.common.url /data/my_local_common
回答by LuGo
git config --file=.gitmodules -e
opens the default editor in which you can update the path
git config --file=.gitmodules -e
打开默认编辑器,您可以在其中更新路径