2

I would like to find out the current commit id of a submodule. I've found this answer about current commit id of specified submodule It suggests this code:

git rev-parse @:./yourSubmodule

It works, but if I update the submodule, it still gives me the old commit id:

git submodule update --remote --merge

git rev-parse @:./yourSubmodule

git submodule status gives me the correct commit id, but the result is not just the commit id, but some auxiliary informations as well:

git submodule status | grep yourSubmodule

Result:

+1b2377f523dca6fa0c49bd7fa56eeb32011774e1 yourSubmodule (remotes/origin/HEAD)

What is the correct way, to determine the current commit id of a submodule? Only the commit id is needed, nothing else.

Iter Ator
  • 8,226
  • 20
  • 73
  • 164

1 Answers1

0

Unfortunately I am afraid that from the superproject you cannot retrieve that information using a simple git command with some option. The first command you found

git rev-parse @:./yourSubmodule

refers to the submodule gitlink in the index of the HEAD (@) revision. However it cannot work in your case because you have not even staged the submodule update. As @torek wrote in the comments, the probably easier way is to run rev-parse from your submodule directory, like this

git -C ./yourSubmodule rev-parse HEAD

But there are several other ways to do this:

  • sedding from git submodule status

    git submodule status | sed -n 's/+\(\w*\) yourSubmodule.*/\1/p'
    
  • DO NOT DO THIS, but that info is stored in the .git repository at .git/modules/yourSubmodule/refs/heads/master before being staged. You can obviously find it elsewhere, for example in FETCH_HEAD, logs folder ecc.

  • Instead, when you stage the submodule reference update, you can retrieve it using a syntax similar to the one that you initially tried:

    git rev-parse :0:./yourSubmodule
    

    that 0 between : is the revision for the stage.

By the way, it is weird that git does not provide a way to retrieve that from the superproject using some subcommand of git submodule, I am still doubtful that I missed it.

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42