gitgit-submodulesgit-commit

How to see which commit a git submodule points at


As far as I understand, if you add a submodule in git, then the main repo contains a pointer to a certain commit of the submodule.

Is there any way to see to which commit the main repo points at for a specific submodule, without checking out the code of the submodule?


Solution

  • Sure; there are several ways:

    1. git ls-tree <commit> <relative path to submodule>
    2. git ls-tree <commit>:<absolute path to parent of submodule>
    3. git ls-tree <commit>:./<relative path to parent of submodule>
    4. git ls-tree -r <commit> <relative path to parent of submodule>

    The first is easiest by far for checking a single submodule.

    The second/third is easiest if you want to check several submodules contained in the same directory, but you don't want to recurse into any other subdirectories that are not submodules. (The : syntax uses the top level of the repository as its reference point by default, rather than your current working directory, so be sure to include the ./ prefix if you're way in the depths of your repo right now.)

    The fourth is easiest if you have a lot of submodules, and only submodules, in a single directory.

    Examples:

    • git ls-tree HEAD src/thirdparty/libfoo shows the submodule and nothing else.
    • git ls-tree HEAD:src/thirdparty gives you everything directly below src/thirdparty, including your submodule src/thirdparty/libfoo.
    • git ls-tree -r HEAD src/thirdparty gives you everything directly below src/thirdparty, including your submodule src/thirdparty/libfoo, but will also recurse into src/thirdparty/docs which is actually a regular directory in your repo.
    • git ls-tree -r HEAD lists absolutely everything in your repo, including submodules.

    Submodules will show up as type commit (as opposed to the usual blob or tree).