git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Why do submodules detach HEAD? (was Re: Incorrect and inconsistent End-Of-Line characters in .gitmodules ...)

From
Atharva Raykar <raykar.ath@gmail.com>
Date
Jul 21, 2021, 07:26 UTC
Message-ID
<46F5B91F-4DBE-4F34-9395-7CC808FAC359@gmail.com>
In-Reply-To
<CANt7McHrYhSe3JsS8UKX8NgsUajwxQY4h9KTtXkEXdd0Be_+yw@mail.gmail.com>
On 18-Jul-2021, at 02:52, Rostislav Krasny <rosti.bsd@gmail.com> wrote:
Show 11 quoted lines
> 
> 
> [...]
> I would like to take this opportunity to ask: why cloning a repository
> that contains submodules sets this repository branch to its default
> (master, main, whatever) but all submodules are set into a detached
> HEAD mode instead of their default branches? This is actually the
> reason I started to check what happens with the .gitmodules file. What
> is the point in such an inconsistent behavior? There are a lot of
> questions about that in Google and it seems most of the users expect
> different behavior.

Submodules are cloned along with the parent repository only when 'submodule.recurse' is set to true, which I assume is the case for your configuration.

A git clone that recurses submodules is equivalent to running 'git submodule update --init --recursive' right after the parent project has been cloned.

The default mode for the update command is to checkout to the revision that the parent project expects the submodule to be in, ie, it checks out to the commit hash directly, and detaches the HEAD.

Why not checkout to a branch? This is because branches may change which commits they point to.

For example, if you had a submodule 'foo'. I go into the 'foo' folder and amend the last commit.

Now if I run 'submodule update' in the parent repository. If it had checked out to the branch instead of the revision, I would be on a different commit in 'foo' than the one that was registered by the parent repo!

We want to have idempotence, ie, for a particular revision that is registered by our parent project, we want 'update' to give the same outcome every time, and not be dependent on whatever the state of the branch is. This way we ensure that for a particular commit in the parent project, the submodules will be in the same state for every system in the world, after an 'update' is run.

I hope that makes sense.

(Anyone more knowledgeable than me in submodules, feel free to add to what I am saying and correct me if I made an error!)

--- Atharva Raykar ಅಥರ್ವ ರಾಯ್ಕರ್ अथर्व रायकर

Previous: Rostislav KrasnyNext: Philippe Blain
Message 4 of 10 in “Incorrect and inconsistent End-Of-Line characters in .gitmodules after "git submodule set-branch --branch <branch_name>"”
  1. Rostislav KrasnyJul 15, 2021
  2. brian m. carlsonJul 16, 2021
  3. Rostislav KrasnyJul 17, 2021
  4. Why do submodules detach HEAD? (was Re: Incorrect and inconsistent End-Of-Line characters in .gitmodules ...)Atharva Raykar, Jul 21, 2021
  5. Philippe BlainJul 21, 2021
  6. Rostislav KrasnyAug 5, 2021
  7. Atharva RaykarAug 5, 2021
  8. Torsten B��gershausenJul 17, 2021
  9. Rostislav KrasnyJul 17, 2021
  10. Torsten B��gershausenJul 18, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.