28

When I add a Git submodule to a Git repository like this,

git submodule add ssh://server/proj1/ proj1
git submodule init
git submodule update

the added submodule will be in detached HEAD mode. I don't know well what that is, but I know that the submodule will be linked to specific revision of the target repository.

I don't know how it actually works, anyway it looks like a proxy branch exists there. I solved this by switching to master branch.

cd proj1
git checkout master

This will switch current branch actual master HEAD, but this does not update the linkage. So If you clone the whole repository again, it will still be linked to old revision.

If I want to make it to be linked to most recent revision (HEAD) always, what should I do?

eonil
  • 83,476
  • 81
  • 317
  • 516
  • Note that submodule now can track a branch. see my edited answer. – VonC Apr 03 '13 at 09:08
  • I found this http://stackoverflow.com/a/20797186/1808261 informative after arriving at this post. – dtmland Mar 24 '15 at 20:16
  • Possible duplicate of [How can I reconcile detached HEAD with master/origin?](http://stackoverflow.com/questions/5772192/how-can-i-reconcile-detached-head-with-master-origin) – Julio Marins May 23 '16 at 15:43

1 Answers1

34

Update March 2013

Git 1.8.2 added the possibility to track branches.

"git submodule" started learning a new mode to integrate with the tip of the remote branch (as opposed to integrating with the commit recorded in the superproject's gitlink).

# add submodule to track master branch
git submodule add -b master [URL to Git repo];

# update your submodule
git submodule update --remote 

See also the Vogella's tutorial on submodules.


Original answer (December 2011)

added submodule will be in detached HEAD mode

Yes, a submodule is about referencing a specific commit, and not a branch.
So:

  • If you checkout a commit SHA1 (or a tag), you are in a detached HEAD mode.
  • If you checkout a branch (like you did with master branch of the submodule), you can create other commits on top of that branch (but you will have to go back to the parent repo in order to commit said parent as well, for you need to record the new submodule commit you created)

See "True nature of submodules" for more.

If you always wanted the latest commit of another repo, the simplest way would be to merge them together (for instance with subtree merging).
See "Merge 2 same repository GIT" for the details and references.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note that git submodules are very different than svn externals for instance. See http://stackoverflow.com/questions/3131912/why-are-git-submodules-incompatible-with-svn-externals/3132221#3132221 – VonC Dec 27 '11 at 12:00
  • 1
    A note just for later. The `subtree merge` was not for me, and I think `submodule` would be the answer if it supports tracking well. – eonil Dec 04 '13 at 22:43
  • @Eonil but it does support tracking very well, if you want it too. simply, its primary function is to record a fixed version. – VonC Dec 05 '13 at 06:46
  • Would anyone be able to help? I'm following these two Git 1.8.2 commands and every time I do a fresh clone of the repo, the submodules still become detached instead of pointing to our "development" branch. Any ideas? – FateNuller Aug 08 '16 at 21:06
  • 2
    @FateNuller Yes, that is the very nature of a submodule: it is *always* detached. You can add a directive in the .gitmodules file to instruct it to update, but that doesn't change the fact the parent repo records a SHA1 for that submodule, and any subsequent clone will checkout the submodule at that SHA1, detached. – VonC Aug 08 '16 at 21:14
  • 1
    @VonC Yeah, I just wish there was an option to tell git to automatically check out a specific branch for each submodule. – electronix384128 May 01 '17 at 23:02
  • Yeah using commit instead of branch is a bummer. It forced me to switch from submodules to vcstool (https://github.com/dirk-thomas/vcstool) – mc.dev Nov 28 '18 at 03:15