Git 不会初始化/同步/更新新的子模块

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

Git will not init/sync/update new submodules

gitgit-submodules

提问by David Eyk

Here's part of the contents of my .gitmodulesfile:

这是我的.gitmodules文件的部分内容:

[submodule "src/static_management"]
        path = src/static_management
        url = git://github.com/eykd/django-static-management.git
[submodule "external/pyfacebook"]
        path = external/pyfacebook
        url = http://github.com/sciyoshi/pyfacebook.git

However, .git/configonly contains the first:

但是,.git/config只包含第一个:

[submodule "src/static_management"]
        url = git://github.com/eykd/django-static-management.git

The second submodule (external/pyfacebook) was added by another developer in a feature branch. I've inherited the development now, and have checked out the feature branch. However, Git will not pull the submodule for me. I've tried:

第二个子模块 ( external/pyfacebook) 是由另一个开发人员在功能分支中添加的。我现在已经继承了开发,并检查了功能分支。但是,Git 不会为我拉取子模块。我试过了:

  • git submodule init
  • git submodule update
  • git submodule update --init
  • git submodule sync
  • Removing all submodule definitions from .git/configand running git submodule init. It only copies over the previously existing submodule and ignores the new one.
  • Entering new submodule definitions in .git/configmanually and running git submodule update. Only the previously existing submodules bother to update.
  • git submodule init
  • git submodule update
  • git submodule update --init
  • git submodule sync
  • 从 中删除所有子模块定义.git/config并运行git submodule init. 它只复制先前存在的子模块并忽略新的子模块。
  • .git/config手动输入新的子模块定义并运行git submodule update. 只有以前存在的子模块才懒得更新。

in various combinations, but git simply will not update .git/configbased on the new contents of .gitmodules, nor will it create the external/pyfacebookfolder and pull the submodule's contents.

在各种组合中,但 git 不会.git/config根据 的新内容进行更新.gitmodules,也不会创建external/pyfacebook文件夹并拉取子模块的内容。

What am I missing? Is manual intervention (adding a submodule entry by hand to .git/config) truly required, and why?

我错过了什么?是否.git/config真的需要手动干预(手动添加子模块条目),为什么?

Edit:Manual intervention does not work. Manually adding the new submodule entry to .git/configdoesn't do a thing. The new submodule is ignored.

编辑:手动干预不起作用。手动添加新的子模块条目.git/config没有任何作用。新的子模块被忽略。

采纳答案by Quickredfox

Did you recently upgrade to git version 1.7.0.4 ? I did and am now having similar issues...

您最近是否升级到 git 版本 1.7.0.4 ?我做了,现在有类似的问题......

Edit: I fixed my problem but have absolutely no idea whatsoever where the problem was. I manually removed submodule entries from both .git/config and .gitmodules and re-added my submodules with the ususal steps (git submodule add etc...) ... Worksforme but adds no value to this thread.

编辑:我解决了我的问题,但完全不知道问题出在哪里。我手动从 .git/config 和 .gitmodules 中删除了子模块条目,并使用通常的步骤(git submodule add 等...)重新添加了我的子模块......

回答by Dave James Miller

I had this same problem - it turned out that the .gitmodules file was committed, but the actual submodule commit (i.e. the record of the submodule's commit ID) wasn't.

我遇到了同样的问题 - 结果是 .gitmodules 文件已提交,但实际的子模块提交(即子模块提交 ID 的记录)并未提交。

Adding it manually seemed to do the trick - e.g.:

手动添加它似乎可以解决问题 - 例如:

git submodule add http://github.com/sciyoshi/pyfacebook.git external/pyfacebook

(Even without removing anything from .git/config or .gitmodules.)

(即使没有从 .git/config 或 .gitmodules 中删除任何内容。)

Then commit it to record the ID properly.

然后提交它以正确记录ID。

Adding some further comments to this working answer: If the git submodule init or git submodule update does'nt work, then as described above git submodule add url should do the trick. One can cross check this by

为这个工作答案添加一些进一步的评论:如果 git submodule init 或 git submodule update 不起作用,那么如上所述 git submodule add url 应该可以解决问题。可以通过以下方式交叉检查

 git config --list

and one should get an entry of the submodule you want to pull in the result of the git config --list command. If there is an entry of your submodule in the config result, then now the usual git submodule update --init should pull your submodule. To test this step, you can manually rename the submodule and then updating the submodule.

并且应该获得您想要在 git config --list 命令的结果中提取的子模块的条目。如果在配置结果中有你的子模块条目,那么现在通常的 git submodule update --init 应该拉你的子模块。要测试此步骤,您可以手动重命名子模块,然后更新子模块。

 mv yourmodulename yourmodulename-temp
 git submodule update --init

To find out if you have local changes in the submodule, it can be seen via git status -u ( if you want to see changes in the submodule ) or git status --ignore-submodules ( if you dont want to see the changes in the submodule ).

要查看子模块中是否有本地更改,可以通过 git status -u (如果您想查看子模块中的更改)或 git status --ignore-submodules (如果您不想查看更改)来查看子模块)。

回答by palik

git version 2.7.4. This command updates local code git submodule update --init --force --remote

git 版本 2.7.4。此命令更新本地代码 git submodule update --init --force --remote

回答by Alex Ivasyuv

Had the same issue, when git ignored initand updatecommands, and does nothing.

有同样的问题,当 git 忽略initupdate命令时,什么都不做。

HOW TO FIX

怎么修

  1. Your submodule folder should be committed into git repo
  2. It shouldn't be in .gitignore
  1. 您的子模块文件夹应该提交到 git repo
  2. 它不应该在 .gitignore 中

If that requirements met, it will work. Otherwise, all commands will execute without any messages and result.

如果满足该要求,它将起作用。否则,所有命令都将执行而没有任何消息和结果。

If you did all that, and it still doesn't work:

如果你做了所有这些,但它仍然不起作用:

  1. Add submodule manually, e.g. git submodule add git@... path/to
  2. git submodule init
  3. git submodule update
  4. commit and push all files - .gitmodulesand your module folder (note, that content of folder will not commit)
  5. drop your local git repo
  6. clone a new one
  7. ensure that .git/configdoesn't have any submodules yet
  8. Now, git submodule init- and you will see a message that module registered
  9. git submodule update- will fetch module
  10. Now look at .git/configand you will find registered submodule
  1. 手动添加子模块,例如 git submodule add git@... path/to
  2. git submodule init
  3. git submodule update
  4. 提交并推送所有文件 -.gitmodules以及您的模块文件夹(注意,文件夹的内容不会提交)
  5. 删除您本地的 git 仓库
  6. 克隆一个新的
  7. 确保.git/config还没有任何子模块
  8. 现在,git submodule init- 您将看到一条消息,该模块已注册
  9. git submodule update- 将获取模块
  10. 现在看,.git/config你会发现注册的子模块

回答by Carlo Wood

There seems to be a lot of confusion here (also) in the answers.

答案中似乎(也)有很多混乱。

git submodule initis notintended to magically generate stuff in .git/config (from .gitmodules). It is intended to set up something in an entirely empty subdirectory after cloning the parent project, or pulling a commit that adds a previously non-existing submodule.

git submodule init打算神奇生成的东西的.git /配置(从.gitmodules)。它的目的是在克隆父项目后在一个完全空的子目录中设置一些东西,或者拉一个添加以前不存在的子模块的提交。

In other words, you follow a git cloneof a project that has submodules (which you will know by the fact that the clone checked out a .gitmodules file) by a git submodule update --init --recursive.

换句话说,您git clone通过git submodule update --init --recursive.

You do notfollow git submodule add ...with a git submodule init(or git submodule update --init), that isn't supposed to work. In fact, the add will already update the appropriate .git/config if things work.

没有git submodule add ...git submodule init(或git submodule update --init)后面,这不应该起作用。事实上,如果一切正常,添加将已经更新适当的 .git/config。

EDIT

编辑

If a previously non-existing git submodule was added by someone else, and you do a git pullof that commit, then the directory of that submodule will be entirely empty (when you execute git submodule statusthe new submodule's hash should be visible but will have a -in front of it.) In this case you need to follow your git pullalso with a git submodule update --init(plus --recursivewhen it's a submodule inside a submodule) in order to get the new, previously non-existing, submodule checked out; just like after an initial clone of a project with submodules (where obviously you didn't have those submodules before either).

如果其他人添加了以前不存在的 git 子模块,并且您执行了git pull该提交,则该子模块的目录将完全为空(当您执行git submodule status新子模块的哈希应该是可见的,但会-在前面有一个它。)在这种情况下,你需要在你的后面加上git pull一个git submodule update --init--recursive当它是子模块中的子模块时加上),以便检出新的、以前不存在的子模块;就像在对带有子模块的项目进行初始克隆之后一样(显然您之前也没有这些子模块)。

回答by masterop

I had the same problem but none of the solutions above helped. The entries in the .gitmodules and in .git/config were right but the command git submodules update --init --recursivewas doing nothing. I also removed the submodule directory and did run git submodules update --init --recursiveand got the submodule directory back but with exactly the same commit as before.

我遇到了同样的问题,但上述解决方案都没有帮助。.gitmodules 和 .git/config 中的条目是正确的,但命令git submodules update --init --recursive什么也没做。我还删除了子模块目录并确实运行git submodules update --init --recursive并取回了子模块目录,但提交与以前完全相同。

I found the answer on this page. The command is:git submodule update --remote

我在这个页面上找到了答案。命令是:git submodule update --remote

回答by Levi Figueira

Sort of magically, but today I ran git submodule initfollowed by git submodule syncfollowed by git submodule updateand it started pulling my submodules... Magic? Perhaps! This is truly one of the most annoying experiences with Git…

有点神奇,但今天我跑了git submodule initgit submodule sync接着是git submodule update,它开始拉我的子模块......魔术?也许!这确实是使用 Git 时最烦人的体验之一……

Scratch that. I actually got it working by doing git submodule update --init --recursive. Hope this helps.

抓那个。我实际上是通过做来让它工作的git submodule update --init --recursive。希望这可以帮助。

PS: Make sure you are in the root git directory, not the submodule's.

PS:确保您在根 git 目录中,而不是子模块的。

回答by farinspace

Thinking that manually setting up .gitmodulesis enough is WRONG

认为手动设置.gitmodules就足够了是错误的

My local git version 2.22.0as of this writing.

git version 2.22.0在撰写本文时,我的本地人。

So I came to this thread wondering why wasn't git submodule initworking; I setup the .gitmodulesfile and proceeded to do git submodule init...

所以我来到这个线程想知道为什么不git submodule init工作;我设置了.gitmodules文件并继续执行git submodule init...

IMPORTANT

重要的

  1. git submodule add company/project.git includes/projectis required(when adding the module for the first time), this will:

    • add config to .git/config
    • update the .gitmodulesfile
    • track the submodule location (includes/projectin this example).
  2. you mustthen git commitafter you have added the submodule, this will commit .gitmodulesand the tracked submodule location.

  1. git submodule add company/project.git includes/project必需的(第一次添加模块时),这将:

    • 将配置添加到 .git/config
    • 更新.gitmodules文件
    • 跟踪子模块位置(includes/project在本例中)。
  2. 必须那么git commit您已经添加子模块之后,这将提交.gitmodules和跟踪的子模块的位置。

When the project is cloned again, it will have the .gitmodulesand the empty submodules directory (e.g. includes/projectin this example). At this point .git/configdoes not have submodule config yet, until git submodule initis run, and remember this only works because .gitmodulesAND includes/projectare tracked in the main git repo.

再次克隆项目时,它将具有.gitmodules和 空的子模块目录(例如includes/project在本例中)。在这一点上.git/config还没有子模块配置,直到git submodule init运行,记住这只有效,因为.gitmodulesANDincludes/project在主 git repo 中被跟踪。

Also for reference see:

另请参阅:

回答by dirkaholic

According to the answer from Dave James Miller I can confirm that it worked for me. The important thing here was to commit the subprojects commit ID. Just to have the entry in .gitmodules was not enough.

根据 Dave James Miller 的回答,我可以确认它对我有用。这里重要的是提交子项目提交 ID。仅仅在 .gitmodules 中有条目是不够的。

Here is an appropriate commit:

这是一个适当的提交:

https://github.com/dirkaholic/vagrant-php-dev-box/commit/d5f4c40bdbd80eefbb5ac6029823733f591435ae

https://github.com/dirkaholic/vagrant-php-dev-box/commit/d5f4c40bdbd80eefbb5ac6029823733f591435ae

回答by joseph.hainline

I had the same problem.

我有同样的问题。

.gitmoduleshad the submodule, but after a git submodule initcommand it wasn't in .git/config.

.gitmodules有子模块,但在git submodule init命令之后它不在.git/config.

Turns out the developer who added the submodule also added the submodule directory to the .gitignorefile. That doesn't work.

原来添加子模块的开发人员也将子模块目录添加到.gitignore文件中。那行不通。