英文:
git submodule at a specific commit/tag
问题
**问题**:我可以将git模块固定到特定的提交吗?
我正在编写一个构建脚本,用于生成一个Zip归档文件,其中包含多个子模块并具有一些要求。
- 生成的归档文件必须带有标签/版本信息
- 必须使用具有构建脚本的存储库的标签来对归档文件进行版本控制
这里的关键是:
如果我执行:
git clone git@whatever:thing/mybuildscipt
然后执行
git submodule update --init
我需要将每个子模块的确切提交号记录在构建脚本的存储库中(理想情况下记录在.gitmodules中),以便将来,如果我执行:
git checkout 1.2.3
git submodule update
我将得到与构建脚本存储库在标记为“1.2.3”时相同的子模块。
很久以前,这正是git子模块的工作方式:它们被固定到特定的提交,而且该提交哈希实际上在`.gitmodule`中 - 我需要这种行为,根据我的研究,目前是否仍然可能并不清楚。
以下是在构建脚本中运行的命令的摘要,以便你能了解要实现的目标。最终,如果我检出包含构建脚本的git存储库的特定版本,它应该生成与之前完全相同的Zip归档文件。
```shell
export NEXT_VERSION=$(
git tag --sort=committerdate \
| tail -1 \
| awk -F '.' '{print $1"."$2"." $3 +1}')
git submodule update --init
# 删除.git目录以节省空间
git submodule | awk '{print $2}' | xargs -I {} -n 1 rm -fR {}/.git
git submodule | awk '{print $2}' | xargs zip -qr ${FILE_NAME}
# 推送到对象存储
mc cp ${FILE_NAME} ${ALIAS}/${bucket}/${FILE_NAME}
# 打标签
git tag ${NEXT_VERSION}
# 更新标签
git push --tags origin
英文:
The problem: Can I pin git modules to specific commits?
I am writing a build script to produce an artifact which is a Zip archive of multiple submodules with a few requirements.
- the artifact generated must be tagged / versioned
- the tag of the repo with the build script must be used to version the artifact
Here is the rub:
If I checkout:
git clone git@whatever:thing/mybuildscipt
and then do
git submodule update --init
I need the exact commit number of each submodule to be recorded in the build script's repo (ideally in .gitmodules) so that in the future, if I should do
git checkout 1.2.3
git submodule update
I am going to the same submodules as they were at when the build script repo was tagged with "1.2.3".
Long ago, this is exactly how git submodules worked: They were pinned to a specific commit and that commit hash was actually in the .gitmodule
- I need that behavior and it's not at all clear to me, based on my research, if it's still possible.
Here is a summary of the commands being run the in build script, so that you can get the idea what is to be accomplished. Ultimately if I checkout a specific version of the git repo that contains the build script, it should produce an identical zip archive as before.
export NEXT_VERSION=$(
git tag --sort=committerdate \
| tail -1 \
| awk -F '.' '{print $1"."$2"." $3 +1}')
git submodule update --init
# remove the .git dir to save space
git submodule | awk '{print $2}' | xargs -I {} -n 1 rm -fR {}/.git
git submodule | awk '{print $2}' | xargs zip -qr ${FILE_NAME}
# push to object storage
mc cp ${FILE_NAME} ${ALIAS}/${bucket}/${FILE_NAME}
# tag
git tag ${NEXT_VERSION}
# update tags
git push --tags origin
答案1
得分: 1
在**gitlink**中,子模块的提交哈希存储为索引中的特殊条目3。
git ls-files --stage|grep 160000
子模块的主要目的是记录/恢复其关联存储库的特定SHA12。
如果您的子模块配置为跟随分支,并且您使用git submodule update --remote
选项,则可能会出现不恢复相同SHA1的情况。
英文:
> Where is the commit hash for the submodules stored then?
In a gitlink, a SHA1 recorded as a special entry in the index.
git ls-files --stage|grep 160000
The all purpose of a submodule is to record/restore a specific SHA1 of its associated repository.
One possible scenario where the same SHA1 is not restore is if your submodule is configured to follow a branch, and you are using a git submodule update --remote
option.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论