如何从父克隆中的过去提交中获取git子模块的关联提交ID? [英] How can I get a git submodule's associated commit ID from a past commit in the parent clone?

查看:138
本文介绍了如何从父克隆中的过去提交中获取git子模块的关联提交ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以根据父克隆中的提交ID来确定子模块的SHA-1提交ID?我知道我可以通过'git submodule'找到当前关联的SHA-1。

下面是一个例子:我有一个克隆,其中包含一个子模块'foo'在过去的一个月中有好几次。我在几个星期大的父克隆中有一个标签,叫做'released-1.2.3'。我想知道'foo'的相关SHA-1是用于这个标记提交的。我可以简单地查看'released-1.2.3'并使用git-submodule来查看,但我想知道是否有办法在不影响工作树的情况下执行此操作,因为我想编写它。



我想这样做是因为我想构建一个脚本来对父库中的两个提交之间的子模块内的所有更改进行'差异' - 即告诉我哪些文件内部发生了更改

解决方案

您可以使用 git-ls-tree 来查看在给定提交期间给定路径的SHA-1标识符:

  $ git ls-tree发布-1.2.3 foo 
160000提交c0f065504bb0e8cfa2b107e975bb9dc5a34b0398 foo

(我的第一个想法是 git show released-1.2.3 foo ,但是失败时显示致命:坏对象。)



由于您正在编写输出脚本,因此您可能希望自行获取SHA-1标识,例如:

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

另外:在围绕git编写脚本时,试试坚持 plumbing 命令,如在手册中描述。他们有一个更稳定的界面,而更熟悉的瓷器命令可能会以不兼容的方式进行更改。


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."

解决方案

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.

这篇关于如何从父克隆中的过去提交中获取git子模块的关联提交ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆