git子模块add -b如何工作?
在添加带有特定分支的子模块后,一个新的克隆存储库(在git子模块update——init之后)将在特定的提交处,而不是分支本身(子模块上的git状态显示为“not current on any branch”)。
我在.gitmodules或.git/config中找不到任何关于子模块分支或任何特定提交的信息,那么Git是如何解决的呢?
另外,是否可以指定一个标记而不是分支?
我使用的是1.6.5.2版本。
git子模块add -b如何工作?
在添加带有特定分支的子模块后,一个新的克隆存储库(在git子模块update——init之后)将在特定的提交处,而不是分支本身(子模块上的git状态显示为“not current on any branch”)。
我在.gitmodules或.git/config中找不到任何关于子模块分支或任何特定提交的信息,那么Git是如何解决的呢?
另外,是否可以指定一个标记而不是分支?
我使用的是1.6.5.2版本。
当前回答
我想在这里补充一个答案,它实际上只是其他答案的综合,但我认为它可能更完整。
有了这两个东西,就有了Git子模块。
Your .gitmodules has an entry like so: [submodule "SubmoduleTestRepo"] path = SubmoduleTestRepo url = https://github.com/jzaccone/SubmoduleTestRepo.git You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do git submodule status from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit. Whenever you do a git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the .gitmodules. Now, all the -b does is add one line in your .gitmodules file. So following the same example, it would look like this: [submodule "SubmoduleTestRepo"] path = SubmoduleTestRepo url = https://github.com/jzaccone/SubmoduleTestRepo.git branch = master Note: only branch name is supported in a .gitmodules file, but SHA and TAG are not supported! (instead of that, the branch's commit of each module can be tracked and updated using "git add .", for example like git add ./SubmoduleTestRepo, and you do not need to change the .gitmodules file each time) The submodule object is still pointing at a specific commit. The only thing that the -b option buys you is the ability to add a --remote flag to your update as per Vogella's answer: git submodule update --remote Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository. git submodule add -b is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the .gitmodules file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.
其他回答
注意:Git 1.8.2增加了跟踪分支的可能性。下面是一些答案。
习惯这一点有点令人困惑,但子模块并不在分支上。正如您所说,它们只是指向子模块存储库的特定提交的指针。
这意味着,当其他人签出你的存储库,或提取你的代码,并进行git子模块更新时,子模块将被签出到特定的提交。
这对于不经常更改的子模块非常有用,因为这样项目中的每个人都可以在同一次提交时拥有子模块。
如果你想移动子模块到一个特定的标签:
cd submodule_directory
git checkout v1.0
cd ..
git add submodule_directory
git commit -m "moved submodule to v1.0"
git push
然后,另一个希望将submodule_directory更改为该标记的开发人员执行此操作
git pull
git submodule update --init
Git拉修改提交他们的子模块目录指向。Git子模块update实际上合并到新代码中。
根据我的经验,在超级项目或未来的签出中切换分支仍然会导致子模块的头部分离,无论子模块是否正确添加和跟踪(即@djacobs7和@Johnny Z答案)。
而不是手动签出正确的分支手动或通过脚本git子模块foreach可以使用。
这将检查子模块配置文件中的分支属性并检出set分支。
Git子模块foreach -q——recursive 'branch="$(Git config -f $ topllevel . "gitmodules子模块。name.branch美元)”;Git checkout $branch'
Git子模块add -b develop——name branch-name——https://branch.git
现有的答案忽略了第二步,并且有过多的细节。
切换一个现有的子模块来跟踪一个新的远程url和/或一个新的分支:
在.gitmodules中编辑真相的来源。
例如,从
[submodule "api"]
path = api
url = https://github.com/<original_repo>/api.git
to
[submodule "api"]
path = api
url = https://github.com/<another_repo>/api.git
branch = work-in-progress
你也可以使用hexsha来进行提交哈希。或标签,但见3。
git子模块同步:从.gitmodules中指定的刚刚编辑过的真值源中更新git在.git/modules中缓存的子模块的描述。 只有当你指定一个标签:git submodule foreach——recursive 'git fetch——tags'来获取标签。 git子模块update——init——recursive——remote:更新工作副本中签出的子模块。 提交更改。
Git 1.8.2增加了跟踪分支的可能性。
# add submodule to track branch_name branch
git submodule add -b branch_name URL_to_Git_repo optional_directory_rename
# update your submodule
git submodule update --remote
参见Git子模块