How to Reset All Submodules in Git

Git submodule is a nice and convenient mechanism in Git to include the other Git repositories and manage the version/commit used from the submodule Git repository. However, there are cases where we would like "reset" the submodule, such as when we find updating a submodule repository will break the code tree, or merge some updates from others which update the submodules. In such cases, we may want to reset all submodules to the initial commit.

Method 1: Reset git submodules using foreach command

This method is fast because it loops each submodule and runs git reset --hard within each modules. The command is as follows.

$ git submodule foreach --recursive git reset --hard

However, it may fail under some cases, especially after a merging which remove/add submodules.

Method 2: Reset git submodules by deiniting and initing

If the method 1 does not work, we may use the second method by deinitalizing the submodules and reinitializing the submodules again. This will remove all submodules and reclone them again. So it will take more time than method 1. But this can fix many problems which leads to failures of simplying git reset. The commands are as follows.

$ git submodule deinit -f . # unbinds all submodules
$ git submodule update --init # checkout again
Leave a Reply

Your email address will not be published. Required fields are marked *