42

Probably very silly question, - but I've been specifying submodules up until now in .gitmodules file. It recently struck me that perhaps it's possible to just use .git/config for the same reason so I won't have to keep extraneous file in working directory?

.git/config :

[submodule "path/to/repo"]
    url = git@github.com:username/repo.git

.gitmodules

[submodule "path/to/repo"]
    path = path/to/repo
    url = git@github.com:username/repo.git

Are these basically the same things?

Stann
  • 13,518
  • 19
  • 65
  • 73

2 Answers2

36

Same answer than .git/info/exclude and .gitignore.

The .gitmodules file can be included in the repository and shared with everyone (that is, it can be added and committed like any regular file), whereas anything in .git (like .git/config) is private (you cannot add it in the repository).

Artefact2
  • 7,516
  • 3
  • 30
  • 38
  • 6
    what if I have different repos specified in .git/config and .gitmodules - which one would take precedence? – Stann May 04 '12 at 19:00
  • I have confused, because `.git/config` contains absolute URL always, whether .gitmodules contains relative URLs. – betontalpfa Apr 28 '20 at 08:04
  • @Stann I am a bit late. But, speaking from experience, git will use the path specified in the .git/config. Basically, git reads the .gitmodules file (when explicitly told to do so) and then saves the relevant info in the config. In the event the URL does end up changing, you will need to perform a force init or manually delete the existing submodule section from config and then perform a `submodule init` again. – SNikhill Jul 07 '22 at 06:21
21

The git submodule sync will update your config file with the details from the .gitmodules file, so the latter should be considered the 'master' - it's passed between repos as stated by @Artefact2.

This is useful when submodule URLs change upstream and you need to update your local repositories accordingly.

Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
  • 1
    which one would take precedence if they have different repos specified? – Stann May 04 '12 at 19:10
  • 4
    for the `sync` sub-command it is the .gitmodules that takes precedence, but see the manual for the extra conditions about those not listed in config. – Philip Oakley May 05 '12 at 19:25