Handle Submodule Changes

If you modify files inside a Git submodule but only commit the changes in the parent repository (without committing those changes in the submodule itself), the following will happen:

Key Behavior on Push:

  1. Local Modifications:
    • The changes you made inside the submodule (i.e., modifying files) are considered local modifications in the submodule. Git does not automatically track these changes in the submodule’s repository unless you explicitly stage and commit them within the submodule.
  2. Commit in the Parent Repository:
    • When you commit changes in the parent repository (the main repository), Git only records the submodule reference (i.e., the specific commit hash of the submodule). If you haven’t committed the changes in the submodule itself, Git will continue referencing the previous commit in the submodule.
  3. Push Action:
    • When you push the parent repository, it will push the reference to the submodule as it currently stands. However, any local changes inside the submodule (that haven’t been committed in the submodule) will not be included in this push.
    • The parent repository will still reference the last committed state of the submodule, not the uncommitted changes you made inside it.
  4. Result for Others Cloning the Repository:
    • If someone else clones the repository or pulls your changes, they will get the submodule at the previous state (the last committed submodule version), and your uncommitted local changes will be absent. They won’t see any of the modifications you made within the submodule unless you also commit those changes in the submodule’s own repository.

Summary of What Happens:

  • The parent repository will push the submodule reference (the commit SHA-1) pointing to the last committed state of the submodule.
  • Any uncommitted changes inside the submodule will not be reflected in the push.
  • Other collaborators will not see the changes you made in the submodule unless you commit and push them within the submodule repository itself.

How to Properly Handle Submodule Changes:

  1. Commit Changes in the Submodule: Before committing the parent repository, make sure to commit changes in the submodule:
    • Navigate to the submodule directory.
    • Commit your changes within the submodule.
cd path/to/submodule
git add .
git commit -m "Update submodule content"
git push

2. Update the Submodule Reference in the Parent Repository: After committing in the submodule, navigate back to the parent repository and update the reference.

Add and commit the updated submodule reference.

cd ../
git add path/to/submodule
git commit -m "Update submodule reference"
git push

This ensures that both the submodule and the parent repository are in sync and the changes propagate correctly.