添加 Git 子模块时如何指定分支/标签? [英] How can I specify a branch/tag when adding a Git submodule?

查看:55
本文介绍了添加 Git 子模块时如何指定分支/标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git submodule add -b 如何工作?

添加具有特定分支的子模块后,新的克隆存储库(在 git submodule update --init 之后)将处于特定提交,而不是分支本身(git status<子模块上的/code> 显示当前不在任何分支上").

After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init) will be at a specific commit, not the branch itself (git status on the submodule shows "Not currently on any branch").

我在 .gitmodules.git/config 上找不到关于子模块分支或任何特定提交的任何信息,那么 Git 是如何计算出来的?

I can't find any information on .gitmodules or .git/config about the submodule's branch or any specific commit, so how does Git figure it out?

另外,是否可以指定标签而不是分支?

Also, is it possible to specify a tag instead of a branch?

我使用的是 1.6.5.2 版.

I'm using version 1.6.5.2.

推荐答案

注意:Git 1.8.2 添加了跟踪分支的可能性.请参阅下面的一些答案.

Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.

习惯这个有点令人困惑,但是子模块不在一个分支上.就像你说的,它们只是一个指向子模块存储库特定提交的指针.

It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.

这意味着,当其他人签出您的存储库或提取您的代码并执行 git submodule update 时,子模块将签出到该特定提交.

This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.

这对于不经常更改的子模块非常有用,因为项目中的每个人都可以在同一个提交中拥有子模块.

This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.

如果要将子模块移动到特定标签:

If you want to move the submodule to a particular tag:

cd submodule_directory
git checkout v1.0
cd ..
git add submodule_directory
git commit -m "moved submodule to v1.0"
git push

然后,另一个想要将 submodule_directory 更改为该标签的开发人员执行此操作

Then, another developer who wants to have submodule_directory changed to that tag, does this

git pull
git submodule update --init

git pull 提交其子模块目录指向的更改.git submodule update 实际上合并到新代码中.

git pull changes which commit their submodule directory points to. git submodule update actually merges in the new code.

这篇关于添加 Git 子模块时如何指定分支/标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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