简体   繁体   English

如何重新创建已删除的.gitmodule文件?

[英]How do I re-create a deleted .gitmodule file?

I have a submodule in my repo, and I think I inadvertently deleted the .gitmodules file. 我的存储库中有一个子模块,并且我认为无意中删除了.gitmodules文件。

I don't want to do git submodule add <git@git:...> because that will pull the original repo for the submodule into the folder and erase the changes I made to the submodule. 我不想做git submodule add <git@git:...>因为那样会将子模块的原始存储库拉到文件夹中,并删除我对子模块所做的更改。 So all I want to do is just re-create the .gitmodules file. 因此,我要做的就是重新创建.gitmodules文件。

How do I re-create it? 如何重新创建?

Was the file committed to the repository? 文件是否已提交到存储库? If so, you can probably do something like git checkout -- .gitmodules from the root of the repository. 如果是这样,您可能可以从存储库的根目录执行git checkout -- .gitmodules之类的操作。

If the .gitmodules file was not committed to the repository, you will probably have to rebuild it by hand. 如果.gitmodules文件未提交到存储库,则可能必须手动重建它。 Luckily, most of the information that you need can be found in .git/config : 幸运的是,您需要的大多数信息都可以在.git/config找到:

...
[submodule "vendor/foo"]
        url = https://somesite.tld/user/repository.git
...

For each of your submodules, copy this section from .git/config into .gitmodules . 对于每个子模块,将此部分从.git/config复制到.gitmodules Then add a new line path under each section pointing to the location of the module within your repository: 然后在每个部分下面添加一个新的行path以指向模块在存储库中的位置:

...
[submodule "vendor/foo"]
        path = vendor/foo
        url = https://somesite.tld/user/repository.git
...

That should get you pretty close to where you were before deleting your .gitmodules file. 这应该使您非常接近删除.gitmodules文件之前的.gitmodules You can verify that you've got it right with git submodule status . 您可以使用git submodule status验证您的设置是否正确。 If your paths are set incorrectly, you will get something like 如果您的路径设置不正确,您将得到类似

No submodule mapping found in .gitmodules for path 'vendor/foo'

If your .gitmodules is set up correctly, you should see something like 如果您的.gitmodules设置正确,您应该会看到类似

0123456789abcdef01234567890abcdef0123456 vendor/foo (heads/master)

Now that you've got everything sorted out, commit that .gitmodules file! 现在您已经整理了所有内容,提交.gitmodules文件! It should be part of your repository. 它应该是您的存储库的一部分。

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

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