Most often, the submodules in Git repositories need to be reset. The reasons behind the same could be changes, conflicts, or just to get to an old state. But how to exactly reset a Git submodule?

Multiple ways and code snippets have been provided here for you to understand how to reset Git submodules. Implement these methods effectively to reset Git submodules.

Method #1: Resetting a Single Submodule

The way is: to first position into the sub-directory of the submodule and execute a hard reset. Here is how:

1
git reset --hard

If your submodule still shows “modified content,” you can reset it to the correct base commit using:

1
git submodule update --init

Method #2: Unbinding and Checking Out All Submodules

Another approach to resetting a Git submodule is to unbind and check out all submodules. This method ensures a clean state for all submodules in your repository. Here’s how:

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

This process may take some time, but it effectively resets all submodules in your Git repository.

Method #3: Performing a Hard Reset on All Submodules

You can also perform a hard reset on all submodules simultaneously. This method is useful when you need to revert all submodules to their initial state. Here’s how:

1
git submodule foreach git reset --hard

Alternatively, you can use the recursive flag to apply the reset to all nested submodules:

1
git submodule foreach --recursive git reset --hard

Method #4: Complete Reset and Cleanup

If the above methods don’t work, you can perform a complete reset and cleanup of your submodules. Follow these steps:

  1. Clean up your Git repository:
1
git clean -xfd
  1. Clean up each Git submodule:
1
git submodule foreach --recursive git clean -xfd
  1. Reset your Git repository:
1
git reset --hard
  1. Reset each Git submodule:
1
git submodule foreach --recursive git reset --hard
  1. Update all submodules to the correct base commit:
1
git submodule update --init --recursive

Conclusion

There are different ways to reset a Git submodule. A typical solution to any issues, and one that will get your submodules back to the correct versions again, is to do a reset: management and resetting will be better once you carefully read this tutorial with methods to efficiently reset Git submodules to keep everything on track for development.