git submodule sync 命令 - 它是做什么用的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33739376/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:31:33  来源:igfitidea点击:

git submodule sync command - what is it for?

gitgit-submodules

提问by Narek

I don't understand from the documentationwhat does git submodule syncdo. Please explain.

我从文档中不明白是做git submodule sync什么的。请解释。

回答by db48x

Git stores information about submodules in two places. The first is in a file called .gitmodules, which is checked in to the git repository. Changes to this file are what get propagated to other repositories.

Git 将有关子模块的信息存储在两个地方。第一个位于名为 的文件中.gitmodules,该文件已签入 git 存储库。对此文件的更改会传播到其他存储库。

The other location is in .git/config, and it's where git actually looks when performing most commands.

另一个位置在 中.git/config,它是 git 在执行大多数命令时实际查看的位置。

So imagine you've worked on a project for a while, and there's a submodule called foo. Files for this submodule are checked out from some url, it doesn't really matter where. At some point, however, this url changes. Perhaps it changes so that the submodule is checked out from a different server, or a different path or whatever. When you update your repository your copy of the .gitmodulesfile will be updated, but not your .git/configfile; you would still be checking out from the old url. When you want to start checking out from the new url, you would run git submodule syncto copy the new configuration from .gitmodulesto .git/config. Git doesn't do this automatically because you might have made your own changes to your configuration, and it wouldn't want to overwrite them.

因此,假设您已经在一个项目上工作了一段时间,并且有一个名为foo. 这个子模块的文件是从某个 url 中检出的,它在哪里并不重要。然而,在某些时候,这个 url 会发生变化。也许它会发生变化,以便从不同的服务器或不同的路径或其他任何地方检出子模块。当您更新存储库时,您的.gitmodules文件副本将被更新,但您的.git/config文件不会;您仍然会从旧网址中查看。当您想从新 url 开始检出时,您可以运行git submodule sync以将新配置从 复制.gitmodules.git/config。Git 不会自动执行此操作,因为您可能已经对配置进行了自己的更改,并且它不想覆盖它们。