如何使用Git命令确保重新添加子模块而无需手动更新.gitmodule文件?

5
我正在使用的某个项目中包含了一个 .gitmodules 文件:
(iit_synthesis) brando9~/proverbot9001 $ cat .gitmodules  | grep 'metalib'
[submodule "deps/metalib"]
    path = deps/metalib
    url = git@github.com:plclub/metalib.git

但我非常确定它应该是

[submodule "coq-projects/metalib"]
    path = coq-projects/metalib
    url = git@github.com:plclub/metalib.git

当我通过命令行执行时,由于gitignore文件的原因它不允许我这样做:

(iit_synthesis) brandomiranda~/proverbot9001 ❯ git submodule add --name coq-projects/metalib https://git@github.com:plclub/metalib.git coq-projects/metalib

The following paths are ignored by one of your .gitignore files:
coq-projects
coq-projects/metalib
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

我打算手动修改它,但感觉很不舒服,很奇怪。然后我会强制更新:

git submodule update && git submodule init # todo modify to only target metalib

使用git命令重新添加并更新特定的git模块的正确方法是什么。


应该这样做才是正确的解决方案:

git submodule add -f --name coq-projects/metalib https://github.com/plclub/metalib.git coq-projects/metalib

感觉很巧妙?它总是会起作用吗?


赏金:理想的解决方案,#--在执行标准的git子模块“pulls/inits”之前明确pull metalib(目前希望以后能够修复,使git“pull”完成所有操作)
#-- Pull metalib explicitly 1st before doing the standard git submodule "pulls/inits" (for now hope to fix later so git "pull" does it all)
# - I think this pulls the coq projects properly in proverbot
# todo: Q: metalib missing, how do I pull it with original git submodule commands?
# todo: https://dev59.com/ssXsa4cB1Zd3GeqPmpYF
# todo: https://github.com/UCSD-PL/proverbot9001/issues/59
# todo: https://github.com/UCSD-PL/proverbot9001/issues/60
# ### rm -rf coq-projects/metalib  # why?
git submodule add -f --name coq-projects/metalib https://github.com/plclub/metalib.git coq-projects/metalib

例如:

git submodule update && git submodule init

Bounty2: 需要详细的答案细节以便能够验证建议是否有效

我希望所给出的答案提供更多详细信息,通过提供检查方式来检查更新后的gitmodules是否符合预期,无论是在.gitmodules文件、下载的仓库还是任何检查中。代码和自然语言解释对于验证最为重要。


初始化和更新哪个先执行?

相关说明,我应该何时运行git submodule updategit submodule init真的让我感到困惑。我通常会先运行git submodule init,然后再运行git submodule update --init --remote。这样做正确吗?相关链接: git submodule update或git submodule init应该先运行哪个?


相关:


2
它们被标记为“ignored”(Git选择了一个不太好的动词,但这是另一回事)这一事实非常可疑。如果它们应该作为具有该路径的子模块进行跟踪,则不应将其忽略。修复忽略问题,git submodule命令将停止抱怨。但在这种情况下,这里没有真正的“适当”的东西;Git的子模块目前仍然是一个很多机制但UI很少的东西,因此还没有理由去深入到齿轮和电线中并拉扯和推挤东西。 - torek
@torek 你好,torek!我很困惑,为什么它显示“被忽略”?我不认为我告诉git去忽略任何东西。该选项可能在哪里设置?提前感谢您的时间。不在.gitignore文件中。 - Charlie Parker
顺便提一下,我应该何时运行 git submodule updategit submodule init 真的让我很困惑。我通常会先运行 git submodule init,然后再运行 git submodule update --init --remote。这样做正确吗?相关链接:https://dev59.com/WH8QtIcB2Jgan1zn1Txg - Charlie Parker
请查看以下链接获取有关解决方案:https://github.com/UCSD-PL/proverbot9001/issues/86 - Charlie Parker
请查看以下 Stack Overflow 帖子,了解应该先运行哪个命令:https://dev59.com/WH8QtIcB2Jgan1zn1Txg - joe-bro
显示剩余2条评论
3个回答

1
重新添加 Git 子模块的正确方式是使用以下命令:
git submodule add -f --name <new_submodule_name> <submodule_repository_URL> <submodule_path>

在你的情况下,命令应该是这样的:

git submodule add -f --name coq-projects/metalib https://github.com/plclub/metalib.git coq-projects/metalib

-f标志用于强制添加子模块,即使它被.gitignore文件忽略。

在添加子模块后更新它,您可以运行以下命令:

git submodule update --init coq-projects/metalib

我该如何确认您上面的建议实际起作用了呢? - Charlie Parker
好的,我有几个问题,特别是为了确定你所建议的是否适用于我。 "添加子模块" 在操作上意味着什么,因此我该如何知道它是如何工作的?我的意思是,如果我运行您建议的命令 git submodule add -f --name coq-projects/Metalib https://github.com/plclub/Metalib.git coq-projects/Metalib,我应该看到的更改是 1. 更新 .gitmodules 文件夹并添加上述内容 2. 我应该看到存储库被添加和代码被拉取,还是这就是为什么您建议稍后使用 git submodule update --init coq-projects/Metalib 的原因? - Charlie Parker
顺便说一句,我认为它是metalib而不是Metalib。已经做出了更改。 - Charlie Parker
顺便提一下,我应该何时运行 git submodule update vs git submodule init 真的让我很困惑。我通常会先运行 git submodule init,然后再运行 git submodule update --init --remote。这样做正确吗?相关链接:https://dev59.com/WH8QtIcB2Jgan1zn1Txg 我认为这对我们的讨论很重要。 - Charlie Parker

0

您需要在项目目录中使用git check-ignore --verbose coq-projects命令来获取有关哪个文件包含使git忽略该路径的模式的信息。可能有许多地方包含忽略指令,而不仅仅是.gitignore。找到源并删除该模式后,只需重试您在问题中所做的操作。

更新:实际上,我看到您提供的存储库中忽略了文件夹coq-projects


空白响应表单 (iit_synthesis) brando9~/proverbot9001 $ git check-ignore --verbose coq-projects :( - Charlie Parker
我在.gitignore文件中看到了'/coq-projects/'。这是否解释了为什么.gitmodules不正确,以及为什么我需要运行git submodule add -f --name coq-projects/metalib https://github.com/plclub/metalib.git coq-projects/metalib,以及为什么git submodule init && git submodule update --init --recursive --remote似乎无法正确初始化所有的coq-projects?我很困惑。我的目标是使用理想的标准命令git submodule init && git submodule update --init --recursive --remote一次性拉取所有的coq-projects和正确的版本。你有什么建议吗? - Charlie Parker

0

看这里:https://dev59.com/z9T7oIgBc1ULPQZF4At2#75455526

为了让Daniel的答案更具体,可以这样做:

# -- Get metalib foor coq-8.10 via commit when getting it through git submodules
## git init the right metalib if it wasn't git submodule init properly. ref: https://dev59.com/ssXsa4cB1Zd3GeqPmpYF ref2: https://github.com/UCSD-PL/proverbot9001/issues/59, ref3: https://github.com/UCSD-PL/proverbot9001/issues/60
#rm -rf coq-projects/metalib
#git submodule add -f --name coq-projects/metalib https://github.com/plclub/metalib.git coq-projects/metalib
git submodule add -f --name coq-projects/metalib git+https://github.com/plclub/metalib.git#104fd9efbfd048b7df25dbac7b971f41e8e67897 coq-projects/metalib
git submodule update --init coq-projects/metalib
# cd to coq-projects/metalib and make sure it's using the hash 104fd9efbfd048b7df25dbac7b971f41e8e67897
(cd coq-projects/metalib && git checkout 104fd9efbfd048b7df25dbac7b971f41e8e67897)
(git status && cd ..)

大约输出:

(iit_synthesis) brando9~/proverbot9001 $ rm -rf coq-projects/metalib
(iit_synthesis) brando9~/proverbot9001 $
(iit_synthesis) brando9~/proverbot9001 $ git submodule add -f --name coq-projects/metalib git+https://github.com/plclub/metalib.git#104fd9efbfd048b7df25dbac7b971f41e8e67897 coq-projects/metalib
Reactivating local git directory for submodule 'coq-projects/metalib'.

(iit_synthesis) brando9~/proverbot9001 $
(iit_synthesis) brando9~/proverbot9001 $ cd metalib
-bash: cd: metalib: No such file or directory
(iit_synthesis) brando9~/proverbot9001 $ cd coq-projects/
(iit_synthesis) brando9~/proverbot9001/coq-projects $ cd metalib
(iit_synthesis) brando9~/proverbot9001/coq-projects/metalib $ git rev-parse HEAD
4ea92d82286cf66e54b4119b2bb2b039827204ab
(iit_synthesis) brando9~/proverbot9001/coq-projects/metalib $ cd coq-projects/metalib
-bash: cd: coq-projects/metalib: No such file or directory
(iit_synthesis) brando9~/proverbot9001/coq-projects/metalib $ git checkout 104fd9efbfd048b7df25dbac7b971f41e8e67897
Note: switching to '104fd9efbfd048b7df25dbac7b971f41e8e67897'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 104fd9e Sync Makefile coq version with README/Docker
(iit_synthesis) brando9~/proverbot9001/coq-projects/metalib $ git status
HEAD detached at 104fd9e
nothing to commit, working tree clean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接