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:
foo
that has changed several times in the last month. released-1.2.3
. I want to find out what the associated SHA-1 of foo
was for this tagged commit. 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."
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.
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").
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.
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.
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.
This one worked for me:
$ git rev-parse released-1.2.3^{commit}:foo <SHA1>
Perhaps also quite easy to use in script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With