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
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With