gitgit-submodules

How to revert a Git Submodule pointer to the commit stored in the containing repository?


I have a git submodule in my main git repo. As I understand it, the main repo stores a SHA value (somewhere...), pointing to the specific commit of the submodule that it is "linked to".

I went in to my submodule and typed git checkout some_other_branch. I have no idea which commit I came from.

I would like to revert that pointer so that the main repo and the submodule are in sync again.

My first (probably naive) instinct was to say git reset --hard - that seems to work for everything else. To my surprise, it did not work for this scenario.

So I've figured out that I can type git diff, note the SHA ID that the submodule pointer used to have, and then head into the submodule and git checkout [SHA ID]... but surely there must be an easier way?

As I'm still learning about git submodules, please feel free to correct my terminology if there are words for concepts that I don't know.


Solution

  • You want to update your submodule so it is in sync with what the parent repository believes it should be. This is what the update command is for:

    From the submodule manpage:

    Update the registered submodules, i.e. clone missing submodules and
    checkout the commit specified in the index of the containing
    repository. This will make the submodules HEAD be detached unless
    --rebase or --merge is specified or the key submodule.$name.update
    is set to rebase or merge.
    

    Run this and all should be well:

    git submodule update --init
    

    You can add the --recursive flag as well to recurse through all submodules.