1

By command:

git log SHA1 -1 -p 

I am receiving information about subproject commit SHA2.

Is there any way to print SHA2 with the same git log by using pretty format?

Like

--format="%H | %s | %cD"
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Elcardia
  • 135
  • 10

1 Answers1

0

The submodule commit hash ID you are seeing is part of the patch output. There is no formatting directive to obtain it. The patch shows it because git log -p runs git diff for you.

More specifically, every commit in a repository can be characterized by examining the number of parents the commit has:

  • A commit with no parents is a root commit. This typically is the case for exactly one commit, which is the first one ever made.
  • A commit with two or more parents is a merge commit.
  • The remaining commits, which is usually most of them, are unremarkable or ordinary or some other term—there's no special word to describe them. They have, by definition at this point, exactly one parent commit.

When git log shows you a commit, if it's one of these ordinary single-parent commits and you have enabled -p or --patch, git log runs git diff for you. This diff compares the parent of this commit to this commit. The output is a series of diffs, one for each changed file. The format of each diff is described pretty well in the answers to How to read the output from git diff?, although this question and its answers do not discuss submodules in particular.

When this particular commit has a submodule, and the submodule commit target changes between the parent of this particular commit and this particular commit, you will get as one of your outputs:

diff --git a/path/to/submodule b/path/to/submodule
index aaaaaaa..bbbbbb 160000
--- a/path/to/submodule
+++ b/path/to/submodule
@@@ -1 +1 @@
-Subproject commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+Subproject commit bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

except of course that aaaaaaa and bbbbbbb (and their longer form versions) will be the actual commit hash IDs from the submodule.

The 160000 mode at the end of the index line is the mode used for a tree object of type gitlink. You don't need to care about this, but you do need to understand that a commit stores a tree, because...

These hashes are not part of the main commit objects. They are stored only in the tree associated with the commit (or a subtree of the main tree). You can, however, extract them directly, using git rev-parse. For instance, if the commit you're examining is a123456:

$ git log -1 -p a123456

or:

$ git show a123456

will show you the diff, but:

$ git rev-parse a123456:path/to/submodule

will print out the stored hash ID for commit a123456. To see the other one, inspect the same path in the commit's parent, e.g.:

$ git rev-parse a123456~1:path/to/submodule

(the ~ syntax avoids issues with Windows cmd.exe; in any sane shell, feel free to use a123456^ instead).

torek
  • 448,244
  • 59
  • 642
  • 775