Get the current commit id of specified submodule

Open your shell in the required project folder and then type the following git command:

git submodule

The resulting output would look like:

<module commit> <module-path> (<module-branch)
... 

git rev-parse HEAD:path/to/submodule

I've found this way better than accepted answer's alternatives for my automation usecase, as it outputs only the commit sha of an hipotetical path/to/submodule.

PS: Credits and thanks to heloman


First, as commented by brookbot, git submodule status will print the SHA-1 of the currently checked out commit for each submodule, along with the submodule path and the output of git describe for the SHA-1.
See my other answer below.


You can start with git ls-files -s (as in this answer)

cd /path/to/parent/repo
git ls-files -s yourSubmodule

Note the absence of a trailing '/' after yourSubmodule (which is the root folder of the checked out submodule)

That will give the mode and sha1 associated with the gitlink (special entry in the index of the parent repo)

160000 4d77d23305c5623356955ef9f908f4ec76780ba9 0       yourSubmodule

(The '0' is for the stage number)

Alternatives:

cd /path/to/repo/parentFolder/of/submodule

git ls-tree @ yourSubmodule
git rev-parse @:./yourSubmodule

The rev-parse only returns the submodule SHA1.


As commented by heloman, you can also find the SHA1 with:

git rev-parse HEAD:path-to-your-sub-module

See more in "How to see which commit a git submodule points at".