Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make submodule with detached HEAD to be attached to actual HEAD?

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?

like image 925
eonil Avatar asked Dec 27 '11 08:12

eonil


People also ask

How do you reattach a detached head?

You must understand that any of your branches will not be affected if you ever get into a detached state . Now, the best way to reattach the HEAD is to create a new branch. We can do it as simple as git checkout -b <branch-name> . This will commit the changes from your temporary branch into the branch you need them.

What causes submodule detached head?

So back to the question: Why does it happen? After pulling changes from server, many times my submodule head gets detached from master branch. This is a common case when one does not use submodules too often or has just started with submodules.

Can a submodule point to a branch?

You can set the submodule to track a particular branch (requires git 1.8. 2+), which is what we are doing with Komodo, or you can reference a particular repository commit (the later requires updating the main repository whenever you want to pull in new changes from the module – i.e. updating the commit hash reference).

How do I combine submodules?

In order to update an existing Git submodule, you need to execute the “git submodule update” with the “–remote” and the “–merge” option. Using the “–remote” command, you will be able to update your existing Git submodules without having to run “git pull” commands in each submodule of your project.


1 Answers

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.

like image 122
VonC Avatar answered Oct 14 '22 20:10

VonC