How do I revert my changes to a git submodule?

If you want to do this for all submodules, without having to change directories, you can perform

git submodule foreach git reset --hard

You can also use the recursive flag to apply to all submodules:

git submodule foreach --recursive git reset --hard

Move into the submodule's directory, then do a git reset --hard to reset all modified files to their last committed state. Be aware that this will discard all non-committed changes.


Well for me, having

git reset --hard

just reset the submodule to the state where it checked out, not necessary to the main's repo referenced commit/state. I'll still have "modified contents" like OP said. So, in order to get the submodule back to the corrects commit, I run:

git submodule update --init

Then when I do git status, it's clean on the submodule.


A more fail-safe method than all previous answers:

git submodule deinit -f .
git submodule update --init

The first command completely "unbinds" all submodules, the second then makes a fresh checkout of them.
It takes longer than the other methods, but will work whatever the state of your submodules.