How to reset a git submodule?
When working on a Git repository that contains a submodule, a developer may need to reset it. But how do you do it?
This article explains resetting a Git submodule and gives multiple methods (with code snippets) to achieve it.
Let's get to it 😎.
Note: If you want to REMOVE a submodule, read this article.
Method #1 - Reset a submodule
The easiest method to reset a Git submodule is to navigate into the submodule sub-directory and reset it.
Here is how you can do it:
bashgit reset --hard
If you still have "modified content" in the submodule, you need to get the submodule to the correct base commit like so:
bashgit submodule update --init
Method #2 - Unbind and checkout all submodules
To reset a Git submodule, you can also unbind and check out all of the submodules.
Here is how you can do it:
bashgit submodule deinit -f .
git submodule update --init
This method takes time but should reset all of the submodules in a Git repository.
Method #3 - Do a hard reset on all submodules
To reset a Git submodule, you can also do a hard reset on all of the submodules.
Here is how you can do it:
bashgit submodule foreach git reset --hard
Or using the recursive flag to apply on all the submodules, like so:
bashgit submodule foreach --recursive git reset --hard
Method #4 - Do a complete reset
If none of the previous methods worked, you can try to do a complete reset and cleanup of the submodules in your Git repository.
Here are the steps you need to follow:
1. Clean up your Git repository.
bashgit clean -xfd
2. Clean up each Git submodule.
bashgit submodule foreach --recursive git clean -xfd
3. Reset your Git repository.
bashgit reset --hard
4. Reset each Git submodule.
bashgit submodule foreach --recursive git reset --hard
5. Get all of your submodules to the correct base commit.
bashgit submodule update --init --recursive
Final thoughts
As you can see, resetting a Git submodule is easy!
Most of the time, the complete reset will do the trick for you.
Here are some other Git tutorials for you to enjoy: