用于跟踪远程分支的 Git 子模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19986075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git submodule to track remote branch
提问by Nambi
I'm trying to use git submodules for aggregating 10+ repositories into one structure for easy development.
我正在尝试使用 git 子模块将 10 多个存储库聚合到一个结构中以便于开发。
It is supposed to clone the module and checkout a branch. Instead, the module is checked out in detached head mode.
它应该克隆模块并签出一个分支。相反,模块以分离头模式检出。
git clone [email protected]:org/global-repository.git
git submodule update —init
cd config-framework
git status
$git status
#HEAD detached at b932ab5
nothing to commit, working directory clean
gitmodules files seems to be okay
gitmodules 文件似乎没问题
$cat .gitmodules
[submodule "config-framework"]
path = config-framework
url = [email protected]:org/config-framework.git
branch = MY_BRANCH
We want the MY_BRANCH branch to be checked out by default, rather than detached head. How do we achieve that?
我们希望默认情况下检出 MY_BRANCH 分支,而不是分离头。我们如何做到这一点?
回答by VonC
Submodules are alwayschecked out in a detached HEAD mode.
子模块总是以分离的 HEAD 模式检出。
That is because a submodule will checkout the SHA1 stored in the special entry in the index of the parent repo.
这是因为子模块将检出存储在父 repo 索引中的特殊条目中的SHA1 。
Plus, if you want a submodule to follow the branch you have registered in the .gitmodules
, you need:
另外,如果您希望子模块跟随您在 中注册的分支.gitmodules
,您需要:
git submodule update --init --remote
The --remote
will make a git fetch
, plus a checkout of the new HEAD
.
Alas, even that checkout will be of a commit, not of the branch (since you have no local branch by default in a submodule), so... back to a detached HEAD
mode.
See more at "Git submodules: Specify a branch/tag".
该--remote
会作出git fetch
,再加上新的结账HEAD
。
唉,即使结帐将是提交,而不是分支(因为默认情况下子模块中没有本地分支),所以......回到分离HEAD
模式。
在“ Git 子模块:指定分支/标签”中查看更多信息。
You can try (not tested) a:
您可以尝试(未测试)一个:
git submodule foreach 'git checkout -b $(git config -f /path/to/parent/repo/.gitmodules --get submodule.$path.branch)'
I take advantage of the fact git submodule foreach
has access to '$path
', the name of the submodule directory relative to the superproject.
我利用git submodule foreach
可以访问“ $path
”这一事实,即相对于超级项目的子模块目录的名称。
There was an attempt to specify a branch for a submodule to be automatically checked out in (commit 23d25e4for Git 2.0).... but it got reversed (commit d851ffb, April 2d 2014)!
It might come soon, but not in its current implementation.
曾尝试为要自动检入的子模块指定一个分支(Git 2.0 的commit 23d25e4)......但它被逆转了(commit d851ffb,2014 年 4 月 2 日)!
它可能很快就会出现,但不会在当前的实施中出现。
回答by darKoram
If your plan is to contribute to a submodule, the best approach is to check it out separately as a regular git repo. Do you work in branches with different remotes. Then point your submodule at the single remote you want to use for testing.
如果您的计划是为子模块做出贡献,最好的方法是将其作为常规 git 存储库单独检出。你在不同遥控器的分支机构工作吗?然后将您的子模块指向您要用于测试的单个遥控器。
回答by BartBog
In order to get this done, I always use the following command (slightly based on VonCs proposal earlier:
为了做到这一点,我总是使用以下命令(稍微基于 VonCs 之前的提议:
export top=$(pwd)
git submodule foreach --recursive 'b=$(git config -f ${top}/.gitmodules submodule.${path}.branch); case "${b}" in "") git checkout ${sha1};; *) git checkout ${b}; git pull origin ${b};; esac'
It checks out all submodules (recursively) at their "right" branch (if set) and otherwise at the head as last committed.
它检查所有子模块(递归地)在它们的“正确”分支(如果设置),否则在最后提交的头部。