Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a git submodule's associated commit ID from a past commit in the parent clone?

Is there a way, short of actually checking out the parent commit, to determine a submodule's SHA-1 commit ID based on a commit ID in the parent clone? I know I can find the currently associated SHA-1 with git submodule.

Here's an example:

  • I have a clone with a single submodule foo that has changed several times in the last month.
  • I have a tag in the parent clone that is a few weeks old called released-1.2.3. I want to find out what the associated SHA-1 of foo was for this tagged commit.
  • I could simply check out released-1.2.3 and use git submodule to see, but I'm wondering if there's a way to do this without affecting the working tree, as I want to script it.

I want to do this because I want to construct a script to do a 'diff' on all changes within a submodule between two commits within the parent repository - i.e. "tell me what files changed within the submodule foo between these two commits in the parent."

like image 685
davidA Avatar asked Oct 21 '10 02:10

davidA


People also ask

How do I point a submodule to a specific commit?

Use the git submodule update command to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.

Where is git submodule commit stored?

It is stored in Git's object database directly. The tree object for the directory where the submodule lives will have an entry for the submodule's commit (this is the so-called "gitlink").

What are git submodules?

A git submodule is a record within a host git repository that points to a specific commit in another external repository. Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

How do you clone with submodules?

Git clone with submodules The list of steps required to clone a Git repository with submodules is: Issue a git clone command on the parent repository. Issue a git submodule init command. Issue a git submodule update command.


2 Answers

You may use git-ls-tree to see what the SHA-1 id of a given path was during a given commit:

$ git ls-tree released-1.2.3 foo 160000 commit c0f065504bb0e8cfa2b107e975bb9dc5a34b0398  foo 

(My first thought was git show released-1.2.3 foo, but that fails with "fatal: bad object".)

Since you are scripting the output, you will probably want to get just the SHA-1 id by itself, e.g.:

$ git ls-tree released-1.2.3 foo | awk '{print $3}' c0f065504bb0e8cfa2b107e975bb9dc5a34b0398 

Also: When writing scripts around git, try to stick to the plumbing commands, as described in the manual. They have a more stable interface, while the more familiar “porcelain” commands will possibly change in incompatible ways.

like image 178
Josh Lee Avatar answered Sep 22 '22 02:09

Josh Lee


This one worked for me:

$ git rev-parse released-1.2.3^{commit}:foo <SHA1> 

Perhaps also quite easy to use in script.

like image 31
mash5 Avatar answered Sep 20 '22 02:09

mash5