Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

git - Get hash of submodule at previous version of main repo without checking out

I have a repo which a submodule in it.

I can get the current hash of the submodule from the working directory easily:

cd submodule
git rev-list HEAD | head -n 1

However i am also interested in the hash at previous versions of the main repo. I can get it if i actually check out that version:

git checkout some_tag
cd submodule
git rev-list HEAD | head -n 1

However, i cannot use git checkout. Is there a way to obtain that hash without checking out the old tag?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use git rev-parse (which is also the correct tool for reading the value of HEAD):

git rev-parse some_tag:submodule

This reads the gitlink entry from the tree object to which some_tag can be resolved, using the supplied path. To see how it works, use:

tree=$(git rev-parse some_tag^{tree})
git ls-tree -r $tree

(the -r option is only required if the submodule path is not at the top level of the repository). Note that the tree entry for the submodule is simply 160000 commit <hash> <path>: this is how the superproject knows which commit hash ID to use in the submodule.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...