Unscrewing Git Submodule Updates

Sometimes when working with git submodules, submodules may be accidentally updated and committed. If you end up in such a situation where a submodule was accidentally bumped forward on you, and you’re in a bad state, here’s how you can roll things back.

  1. Checkout a previous commit where things worked

    git checkout COMMIT-HASH-HERE

    You can find the hash for your target commit by looking through your commit log.

  2. Take a look at what commit hashes your submodules are set to

    git submodule status

    Either copy these hashes, or open another terminal for the remainder of the steps.

  3. Switch back to the head of your branch

    git checkout BRANCH-NAME

  4. Go into each submodule directory, and checkout the associated commit hashes you’ve identified in step 2.

    cd submodule-dir && git checkout WORKING-COMMIT-HASH

  5. Verify everything works. Build and/or test your project.

  6. Add and commit the changes you made to your submodule hashes.

Submodules can be tricky to use if you’re not used to them. The above steps got me out of quite a pickle today, and I hope it saves you some time.