0

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
3
  • You can do that but the superproject will note that and on git status will report the submodule as modified. Commented Sep 8, 2021 at 16:46
  • @phd and it creates problem? my main question is if using submodules I can work on all the submodules repository, or if I have to clone the submodules repositories on other folder, update them there and then run submodule update Commented Sep 8, 2021 at 17:46
  • If the remembering the new commits in the superproject is what you need then no problem, you can work directly in the submodule directory. Just don't forget — you must push new commits in the submodule before pushing the superproject to avoid the remote repo for superproject reference non-existing commit in a submodule. Commented Sep 8, 2021 at 19:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much, very clean explanation.. just as a note, testing this turns out to be important to add --remote --merge when running git submodules update otherwise it will checkout a "detached head"... just for those how are reading this and wants to use the submodules as working directory
Right - the git submodule update is the classic (cd $path && git checkout $hash) case, but recursive checkout also does that. Note that detached HEAD mode is normal in submodules! The --remote --merge flags will keep your current branch, but will run git merge as needed as well, creating new commits, which may not be what you want.
@torek what if you don't have rights to push changes to the submodule repository, such as some external GitHub repository you've included as a submodule in your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.