添加Git子模块时如何指定分支/标签?

问题描述:

git submodule add -b如何工作?

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

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子模块更新时,该子模块将签出到该特定提交.

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.