Reputation: 10738
I have a parent project with the following .gitmodules
file
[submodule "src/redux"]
path = src/redux
url = [email protected]:username/sub-package.git
branch = master
Here are the steps I'm using to clone the parent project to ensure I have the submodule set up.
git clone [email protected]:username/parent-project.git
git submodule init
git submodule update
After that, I have the contents of the submodule in the place in the parent project. However, the HEAD is detached in the submodule. First indication that I'm not doing something right. I expect this to be on the master branch since the parent project's .gitmodules
file points to the master branch.
I then checked out master and set up the submodule's master branch to track the remote master branch.
git checkout master
git branch -u origin/master master
When i back out to the parent project and run git status i have an uncommitted change in the submodule.
I feel like I shouldn't have that change when I'm just setting it all up initially. If I did, all developers that cloned the project would also have their own unique initial commit when setting up their local environment. What am I doing wrong?
Upvotes: 4
Views: 2274
Reputation: 1264
Submodules are a bit tricky to work with; if you have them, you need to make sure every dev on your team understands the Git model and how it handles submodules.
When you add a submodule to a repo, the contents of the submodule never get added to the repo itself; the only thing the parent repo ever records about a submodule is the commit hash it corresponds to. You can see this if you run git diff -- src/redux
.
What this means is that when you git submodule init
, you're updating the working tree of the submodule to reflect the hash stored in the index; from git-submodule(1)
:
init
Initialize the submodules recorded in the index (which were added
and committed elsewhere) by copying submodule names and urls from
.gitmodules to .git/config. Optional <path> arguments limit which
submodules will be initialized. It will also copy the value of
submodule.$name.update into .git/config. The key used in
.git/config is submodule.$name.url. This command does not alter
existing information in .git/config. You can then customize the
submodule clone URLs in .git/config for your local setup and
proceed to git submodule update; you can also just use git
submodule update --init without the explicit init step if you do
not intend to customize any submodule locations.
.gitmodules
stores additional information about the submodule configuration, and specifically submodule.src/redux.branch
is used when you update the submodule, i.e. point it at a more recent commit; also from git-submodule(1)
:
--remote
This option is only valid for the update command. Instead of using
the superproject’s recorded SHA-1 to update the submodule, use the
status of the submodule’s remote-tracking branch. The remote used
is branch’s remote (branch.<name>.remote), defaulting to origin.
The remote branch used defaults to master, but the branch name may
be overridden by setting the submodule.<name>.branch option in
either .gitmodules or .git/config (with .git/config taking
precedence).
Upvotes: 1