Resetting a Submodule

Content

Resetting a Submodule

Posted in:

I use submodules a lot to pull in the various GitHub hosted FOSS projects that are incorporated into KosmicTask. My approach in using them is:

  1. Fork the target repo on GitHub.
  2. Make a local clone.
  3. Create a submodule in my project hierarchy that references the local clone

I like to reference the local clone as it adds an extra layer of indirection. I can push and pull changes to GitHub from the local clone to resolve issues, only pulling them into the submodule when necessary. Submodules are created headless so to be able to update them via git pull it is necessary to checkout a branch in the submodule folder once it has been initialised.

The norm with submodules, of course, is not to edit them. Sometimes though a quick edit of a submodule is useful to highlight an issue or experiment with a fix, especially if it contains a linked framework. Resetting the submodule after the illicit edit is normally a matter of git reset --hard. The previous edit can then be applied to the local clone, pushed to GitHub and pulled into the project folder.

However, if the local clone gets rebased for one reason or another (it may have diverged from its upstream source) then issuing a pull within the submodule may well result in conflicts. We don't want to have to resolve conflicts, we just want out submodule to reflect the state of our local clone. In this case we can reset the submodule to its local origin with git reset --hard origin.