Update a Git Submodule Safely
Restore the recorded dependency
In a trusted fresh clone of the supplied app, inspect .gitmodules first. It contains a fixture-owned local path. Initialization registers the submodule configuration locally; update obtains missing objects and checks out the recorded revision using the default checkout procedure. Here file transport is allowed for a single command only because the lab owns both repositories. Do not globally enable file transport or use this override on unknown projects. In ordinary authorized HTTPS or SSH repositories the override is unnecessary. Review nested dependencies before any recursive update.
git show HEAD:.gitmodulesgit -c protocol.file.allow=always submodule update --init --checkout -- vendor/plantsgit submodule status -- vendor/plantsAdvance the pin deliberately
Restoring a recorded pin is different from upgrading it. In app, check that both working directories are clean. The supplied library has advanced from A to B; its tag lib-v2 identifies B. Fetch that reviewed source, inspect the patch, and switch the child to B. The parent still records A until you stage and commit the new gitlink. The child can be detached for this purpose; do not make unanchored development commits. Review the resulting dependency change in the parent.
git status --shortgit -C vendor/plants status --shortgit -C vendor/plants fetch origin --tagsgit -C vendor/plants diff HEAD lib-v2git -C vendor/plants switch --detach lib-v2git diff --submodule=short -- vendor/plantsgit add -- vendor/plantsgit diff --cached --submodule=short -- vendor/plantsgit commit -m "Upgrade plant library"Check a clean consumer
A new consumer must be able to retrieve the child commit. In the lab, cloning the parent again and running the reviewed initialization checks out B. Ordinary update without --remote uses the recorded pin; --remote instead selects a remote-tracking revision according to configuration and needs review before recording it. If a local child is dirty, stop and preserve its changes before any update. Never use --force to silence a refusal. For a reviewed URL change, inspect .gitmodules and use submodule sync for applicable registered settings before fetching. A missing child commit is a dependency availability problem, not a reason to choose an arbitrary latest revision.
Practice
Compare the parent old and new trees. Confirm only the gitlink changed, then verify the exact child commit from a fresh clone. Why should the child source be published first? Because a parent pointer alone cannot supply the child object.
git ls-tree HEAD -- vendor/plantsgit -C vendor/plants rev-parse HEADgit status --porcelainCheck Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Update a Git Submodule Safely — knowledge check
Reason about dependency revisions, ownership and repository boundaries.
What does ordinary submodule update --checkout request?
What must happen to record a new child revision in parent history?
Why publish the child commit before the parent change?
What should you do if the child has valuable uncommitted changes?
Why is file protocol enabled only for the local fixture command?