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

标签 git

为了获得带有子模块的项目,在克隆项目后运行此命令:

git submodule init

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

然后你跑

git submodule update

它获取子模块存储库的某个提交。

git 如何知 Prop 体要获取哪个提交?这些信息存储在哪里?

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

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

注意:我读了this , thisthis .我可能错过了它,但我没有找到问题的答案。

更新 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

有更好的方法吗?

最佳答案

How does git know which commit in particular to get? Where is this information stored?

此信息作为对象存储在 git 文件存储中。例如,取 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)

我们可以使用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.

然后我们往下走:

$ 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

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

When the above is answered, how do we change this commit number, so that a submodule can be retrieved at a different commit next time?

你进入子模块:

$ cd lib/ansible/modules/core

然后检查你想要的提交:

$ git checkout a18306f

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

$ 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)

并提交新的子模块版本:

$ git commit -m 'updated submodules to new commit' core

关于git - 子模块提交号存储在哪里(以及如何更改它)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35211547/

相关文章:

git - GitHub Web UI 中的 'base' 和 'head' 存储库是什么?

git - Windows 版本的 rxvt Backspace 键无法按预期工作

Git,不同的下游和上游远程分支

git - 我如何在 AzureDevOps 存储库中实现/设置 GitFlow,并在 master 和 develop 上使用 PR 分支策略?

上一次提交更改时的 Git rebase

git - 撤消 git remote 上的设置分支

git - 如何在 git checkout 上只恢复修改过的文件?

git - 在多个 Visual Studio Team Services 项目之间共享一个 git 存储库

git - GIT 命令的简单序列

git - 如何在没有冲突相关错误的情况下修改线性历史中的快照(提交)?