Can you use git diff to see changes in submodules, and if so, how?

Yes, you can use git diff to see changes in submodules, but it’s not as straightforward as seeing changes in a regular git repository. By default, git diff doesn’t show changes within a submodule. It only shows that the submodule’s commit hash has changed.

A submodule in Git is like a Git repository inside another Git repository. It’s useful when you want to include external libraries or other projects into your own project, without needing to clone all the code. The submodule will have its own history, branches, and commits, separate from the parent repository.

Here’s how you can view changes in submodules:

Viewing a Simple Summary of Changes:You can use the --submodule or -S option to show a summary of changes for each submodule. The following command will show a diff, including a summary of changes in submodules:

git diff --submodule

The output will show you which commit the submodule is on before and after the changes, along with a summary of the number of insertions and deletions in the submodule.

Viewing Complete Diff of Submodule Changes:To get a full diff of changes within the submodule, you can use the --submodule=diff option. This will show a full diff of changes for each submodule.

git diff --submodule=diff

Be aware that this can potentially produce a lot of output if you have big submodules.

Viewing Changes Within a Specific Submodule:If you want to see the actual changes made within the submodule, you have to navigate into the submodule directory and use git commands there. For example, if your submodule is named mysubmodule, you would do this:

cd mysubmodule git diff

This would show the changes made within the mysubmodule.

Remember, submodule is like a separate Git repository, so most of the regular Git commands will work inside the submodule. You should ensure to keep the submodules updated using git submodule update --remote for the diff to be useful.

Also, remember to commit any changes in the submodule and to the parent repository’s reference to the submodule. If you don’t, your submodules can get into a ‘detached HEAD’ state, which can be a bit confusing to get out of.