Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing a git submodule

Is this possible? Imagine I have projects Parent & Child. Both are git respositories. Child is a submodule of Parent.

Can I make edits to the version of Child that is inside Parent & commit & push them just like a regular repository?

Or do I need a separate clone of Child somewhere that I make changes to?

Thanks.

like image 224
Christopher Stott Avatar asked Mar 25 '11 00:03

Christopher Stott


2 Answers

You don't need a seperate clone. The sub-module folder is a world of its own. Just edit, commit, branch, and push to your heart's delight.

Git is great that way. :-)

BTW, the parent repository will even detect when changes happen inside the sub-module folder and offer you to commit the current state of the sub-module as the new official reference point for clones of the parent repo.

Important note:

Make sure you do git checkout master (or some other branch) inside the sub-module folder before your start hacking.

Then also make sure when you commit the updated state of the sub-module, that you either push those commits to a public repo, or at least that you don't rebase or otherwise change the history inside the sub-module afterwards - as that would corrupt the parent's reference to the sub-module's history.

Tread with care. (Hat tip to @pjmorse for the reminder.)

Bottomline:

Yes. Developing within a submodule folder is possible and often convenient but not without its risks. Choose your development model wisely

like image 111
Már Örlygsson Avatar answered Sep 20 '22 04:09

Már Örlygsson


According to the documentation:

If you want to make a change within a submodule, you should first check out a branch, make your changes, publish the change within the submodule, and then update the superproject to reference the new commit.

As near as I can tell, if you're working on the branch where the submodule was added, you can edit Child and push back to its repository. But if you're not the developer who added Child to Parent, you're working with a detached head and will need to either check out a separate version of Child to make changes, or make the changes and export the patches (using git format-patch) for someone else to commit (via git am).

like image 20
pjmorse Avatar answered Sep 21 '22 04:09

pjmorse