存储的子模块提交编号在哪里(以及如何更改它)?

And*_*ykh 8 git

要获取包含子模块的项目,请在克隆项目后运行此命令:

git submodule init
Run Code Online (Sandbox Code Playgroud)

此命令.gitmodules将有关模块的信息读入和写入索引.

然后你跑

git submodule update
Run Code Online (Sandbox Code Playgroud)

它获得了子模块存储库的特定提交.

git如何知道特别提交哪个提交?这些信息存储在哪里?

我不理解这一点的原因是因为我可以看到它不在,.gitmodules但我认为它也不太可能在索引中,因为它将它git submodule init写入索引.所以它让这些信息没有合理的地方,我能想到的.

回答上述问题后,我们如何更改此提交编号,以便下次可以在不同的提交中检索子模块?

注意:我读过这个,这个这个.我可能错过了,但我找不到问题的答案.

更新1

我想澄清下面关于larsks的答案.这是很好的答案,但我想澄清以下内容.

在答案中,使用git checkout a18306f命令.我想看看我是否有一个快捷方式来为分支上的最新提交执行此操作.为了运行此命令,我需要这样做:

git branch
## Note the current branch name, let's assume it's master, 
## use this branch name in the following command
git log -1 origin/master
## Now note the commit number. 
## This is where a18306f in the example above comes from in my use case. 
## And finally
git checkout a18306f
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

lar*_*sks 11

git如何知道特别提交哪个提交?这些信息存储在哪里?

此信息作为对象存储在git filestore中.例如,使用ansible存储库,其中包括以下子模块:

$ git submodule 
+ce6619bf5db87f94001625c991d02960109dee2d lib/ansible/modules/core (remotes/origin/stable-2.0.0.1)
+29af26884ea11639f38c145b348afccdb6923285 lib/ansible/modules/extras (remotes/origin/stable-2.0.0.1)
Run Code Online (Sandbox Code Playgroud)

我们可以git cat-file用来查看存储库的内容.我们首先tree在当前提交中找到顶级目录的对象...

$ git cat-file -p HEAD
tree a67c8a8e47ea232c9404cc1a971c6134c4008c65
parent 4b7b3794c9a3876080633eae2166a741a3330b27
author Toshio Kuratomi <toshio@fedoraproject.org> 1454618685 -0800
committer Toshio Kuratomi <toshio@fedoraproject.org> 1454618685 -0800

Fix --diff to respect no_log task parameter.
Run Code Online (Sandbox Code Playgroud)

然后继续努力:

$ git cat-file -p a67c8a8e47ea232c9404cc1a971c6134c4008c65 | grep lib
040000 tree 68fa387f2e92cd18288b548a2007ac7ad4c43849    lib
$ git cat-file -p 68fa387f2e92cd18288b548a2007ac7ad4c43849 | grep ansible
040000 tree 6de49b0c156a5055575332e7fe527c9a5e6ca216    ansible
$ git cat-file -p 6de49b0c156a5055575332e7fe527c9a5e6ca216 | grep modules
040000 tree 94211515f23e286a634a7cdaab7958d286bd145f    modules
$ git cat-file -p 94211515f23e286a634a7cdaab7958d286bd145f
100644 blob ae8ccff5952585ebf8134e2f7d09dbfb63772976    __init__.py
160000 commit e1ec52e365a8fbe95c83db5da3046730c4dc39b2  core
160000 commit 14a62fb5d6771871654aedb4a36e17cf358785dc  extras
Run Code Online (Sandbox Code Playgroud)

最后我们看到两个commit对象编码子模块的当前状态.

回答上述问题后,我们如何更改此提交编号,以便下次可以在不同的提交中检索子模块?

您切换到子模块:

$ cd lib/ansible/modules/core
Run Code Online (Sandbox Code Playgroud)

然后签出你想要的提交:

$ git checkout a18306f
Run Code Online (Sandbox Code Playgroud)

返回父模块,您将在其中看到:

$ cd ..
$ git status
On branch devel
Your branch is up-to-date with 'origin/devel'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   core (new commits)
Run Code Online (Sandbox Code Playgroud)

并提交新的子模块版本:

$ git commit -m 'updated submodules to new commit' core
Run Code Online (Sandbox Code Playgroud)