47

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.

Christopher Stott
  • 1,408
  • 3
  • 15
  • 23
  • See also http://stackoverflow.com/questions/1979167/git-submodule-update/1979194#1979194 – VonC Mar 25 '11 at 05:10

2 Answers2

46

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

Community
  • 1
  • 1
Már Örlygsson
  • 14,176
  • 3
  • 42
  • 53
  • 1
    +1 on doing a checkout first. Also, you want to make sure you have a clean Child repo (with any changes committed) before switching branches or doing other work in Parent. – Walter Mundt Mar 25 '11 at 01:29
  • I haven't run into problems with uncommmitted changes in sub-modules when working in the parent repo when doing simple day-to-day (edit, commit, pull, push) work on the parent. Switching branches and submodule update operations might prove risky, though. Good point! – Már Örlygsson Mar 25 '11 at 01:39
  • It's not a bad idea to develop in a clone, just to reduce your chances of accidentally committed with detached HEAD. You can of course also use hooks to check yourself. – Cascabel Mar 25 '11 at 02:59
8

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).

pjmorse
  • 9,204
  • 9
  • 54
  • 124