18

I want get the current commit id of the specified submodule. I thought when I cd into submodule directory and run git rev-parse HEAD i get this after I noticed this is a superproject current id. Tried git submodule status | grep <submodule_name> also but its to slow to me. Any idea how to get this information little bit faster?

eszik.k
  • 1,729
  • 4
  • 17
  • 40

5 Answers5

31

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".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
14

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)
... 
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
priyanka
  • 159
  • 1
  • 3
5
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

laconbass
  • 17,080
  • 8
  • 46
  • 54
0

git submodule status | grep <submodule_name> also but its to slow to me

Starting with Git 2.16 (Q4 2017, two years after the OP's question), this should be faster (even though my previous answer remains a viable alternative).

See commit a9f8a37, commit 9f580a6 (06 Oct 2017), and commit 74a1064 (29 Sep 2017) by Prathamesh Chavan (pratham-pc).
(Merged by Junio C Hamano -- gitster -- in commit a1bf46e, 06 Nov 2017)

submodule: port submodule subcommand 'status' from shell to C

Mentored-by: Christian Couder
Mentored-by: Stefan Beller
Signed-off-by: Prathamesh Chavan

This aims to make git submodule 'status' a built-in. Hence, the function cmd_status() is ported from shell to C. This is done by introducing four functions: module_status(), submodule_status_cb(), submodule_status() and print_status().

The function module_status() acts as the front-end of the subcommand. It parses subcommand's options and then calls the function module_list_compute() for computing the list of submodules. Then this functions calls for_each_listed_submodule() looping through the list obtained.

Then for_each_listed_submodule() calls submodule_status_cb() for each of the submodule in its list. The function submodule_status_cb() calls submodule_status() after passing appropriate arguments to the funciton. Function submodule_status() is responsible for generating the status each submodule it is called for, and then calls print_status().

Finally, the function print_status() handles the printing of submodule's status.

Function set_name_rev() is also ported from git submodule to the submodule--helper builtin function compute_rev_name(), which now generates the value of the revision name as required.

But.. The way "git submodule status" reports an initialized but not yet populated submodule has not been reimplemented correctly when a part of the "git submodule" command was rewritten in C, which has been corrected with Git 2.26 (Q1 2020)

See commit f38c924 (02 Feb 2020), and commit 3b2885e, commit ace912b (24 Jan 2020) by Peter Kaestle (``).
(Merged by Junio C Hamano -- gitster -- in commit f2dcfcc, 14 Feb 2020)

submodule: fix status of initialized but not cloned submodules

Signed-off-by: Peter Kaestle

Original bash helper for "submodule status" was doing a check for initialized but not cloned submodules and prefixed the status with a minus sign in case no .git file or folder was found inside the submodule directory.

This check was missed when the original port of the functionality from bash to C was done.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

This is not the pretiest, but if you cd cd into the subomdule, you can just git log | head -1

Ivan
  • 1,352
  • 2
  • 13
  • 31