Search code examples
gitgit-submodules

how do I make sure to re-add a submodule correctly with a git command without manually updating .gitmodulefiles?


One of the projects I am using has in it/s .gitmodules file:

(iit_synthesis) brando9~/proverbot9001 $ cat .gitmodules  | grep 'metalib'
[submodule "deps/metalib"]
    path = deps/metalib
    url = [email protected]:plclub/metalib.git

but I am pretty sure it should be:

[submodule "coq-projects/metalib"]
    path = coq-projects/metalib
    url = [email protected]:plclub/metalib.git

when I do it by the command line it doesn't let me due to the gitignore file:

(iit_synthesis) brandomiranda~/proverbot9001 ❯ git submodule add --name coq-projects/metalib https://[email protected]: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"

I am going to modify it manually but it feels dirty, weird. Then I will force it to update:

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

What is the proper way using the git command to re-add from stratch and update a specific git module.


Is the proper solution to do:

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

feels hacky? Will it always work?


Bounty: ideal solution, # -- Pull metalib explicitly 1st before doing the standard git submodule "pulls/inits" (for now hope to fix later so git "pull" does it all)

#-- 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://stackoverflow.com/questions/74757297/how-do-i-make-sure-to-re-add-a-submodule-correctly-with-a-git-command-without-ma
# 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

e.g. with:

git submodule update && git submodule init

Bounty2: Need details in answers to be able to verify suggestions actually work

I'd like that the answers given have more details by providing checks I can do to check that the updated gitmodules is as expected either in the .gitmodules files, downloaded repo or any check. Code and natural language explanations are best for verification.


Which is first init or update?

Related note, when I should run git submodule update vs git submodule init vs is really confusing me. I usually do git submodule init then git submodule update --init --remote. Is that correct or a different order is better? related: which should be ran first git submodule update or git submodule init?


related:


Solution

  • The proper way to re-add a Git submodule is to use the following command:

    git submodule add -f --name <new_submodule_name> <submodule_repository_URL> <submodule_path>
    

    In your case, the command would look like this:

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

    The -f flag is used to force the addition of the submodule even if it is ignored by a .gitignore file.

    To update the submodule after adding it, you can run the following command:

    git submodule update --init coq-projects/metalib