Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git updates submodule with an unamed branch

Tags:

git

I've used git for quite a bit now but stayed away from submodules since I didn't had a good reason to use them. However, recently I began a project which clearly needs to use this feature of git.

However, each time I clone the entire project the submodule ends in a branch with no name. Here are the commands I execute:

git clone <url to project>
git submodule update --init <submodule>
cd <submodule>; git branch

and it prints out:

* (no branch)
  master

I need to do an additional

git checkout master

Now my question is: is this the standard behavior? If not, can you help me out understanding what I'm doing wrong?

Thanks

like image 368
Fred Avatar asked Aug 10 '11 22:08

Fred


People also ask

How do I update submodules recursively?

If you already cloned the project and forgot --recurse-submodules , you can combine the git submodule init and git submodule update steps by running git submodule update --init . To also initialize, fetch and checkout any nested submodules, you can use the foolproof git submodule update --init --recursive .

How do I force git update a submodule?

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

Unlike some other SCMs, commits in Git do not inherently belong to any particular branch. A branch head is like a bookmark for a commit. When you have a branch checked out (i.e. the file .git/HEAD contains a reference to the branch), and you make a commit, Git moves that bookmark forward to point to the new commit.

But this tracking behaviour doesn't apply here. As you may already know, a submodule is pinned to a particular commit; it does not track a branch head. When you update a submodule, Git checks out that particular commit only. That means .git/HEAD contains the commit hash, not a branch ref.

There may be one or more branch heads pointing to this commit, but that's kind of irrelevant. Only when HEAD contains a branch ref, not a commit hash, will git branch show you are on a branch.

like image 187
Ben James Avatar answered Sep 30 '22 01:09

Ben James