quick question, can I use submodule as "cloned repos"?
E.g.:
Can I change the submodule branch, and in case, edit the code and do some work in it and commit them?
Example:
> example-repo
|- index.html
|- submodule -> submodule-repo-1
| |- file1.html
|- submodule -> submodule-repo-2
|- file2.html
and do something like:
git clone https://.../example-repo.git
git submodule update
cd submodule-repo-1
git checkout -b test-branch
echo "idk" > file1.html
git commit -am "test commit"
git push
Yes, you can indeed do that. Remember that—at least for now1—a submodule doesn't really "know" that it is a submodule, so once you cd
into it and start running Git commands (git checkout
, git add
, and so on), you're just working in an ordinary Git repository. What makes this Git repository be a submodule is that once you exit out of it back to the superproject, certain Git commands in that superproject will run:
(cd submodule && git checkout $hash)
for specific hash IDs, for instance.
As a result, after you have made changes in a submodule's working tree, it's a good idea to get them committed and then git push
-ed elsewhere, lest the superproject do something crazy like run git checkout
, or really crazy like remove the submodule entirely.2
1There is ongoing work in Git to make submodules more aware that they are submodules, to help fix items like footnote 2.
2This is pretty dangerous, so we don't normally want it, but: what happens, for instance, if path/to/X
in commit a1234567
is the submodule, but path/to/X
in commit b789abc
is a plain file? Git tries hard not to destroy the submodule (or its clone)—these days, the submodule repository proper gets moved to a .git/modules/
directory within the superproject—but it can be necessary.