【发布时间】:2016-10-19 04:22:34
【问题描述】:
我正在寻找一种情况,其中我有一个带有(可能嵌套的子模块)的 git 结构。对于这些子模块中的每一个,我想分别指定它们是否应该跟踪一个分支 (参见例如Git submodules: Specify a branch/tag)
例如,我的项目可能如下所示:
main.tex
|- submod1 @ master
| |-subsubmod1 @qsdf123
|- submod2 @ master
| |-subsubmod2 @shasha12
|- submod3 @ qsdf321
现在,我想要一种方法来更新我的子模块。
git submodule update --recursive
会将所有子模块更新为其最后记录的 sha(即,它将适用于 subsubmod1、subsubmod2 和 submod3,但对其余部分执行错误的操作。 另一方面
git submodule update --recursive --remote
会将所有子模块更新到关联的分支(默认情况下,master),即它适用于 submod1 和 submod2,但对其余部分执行错误的操作。
有没有办法很好地完成这项工作?
针对第一个答案,我将澄清“做错事”的意思。
这是一个小例子
bartb@EB-Latitude-E5450 ~/Desktop/test $ git init
Initialized empty Git repository in /home/bartb/Desktop/test/.git/
bartb@EB-Latitude-E5450 ~/Desktop/test $ git submodule add ../remote/ submod1
Cloning into 'submod1'...
done.
bartb@EB-Latitude-E5450 ~/Desktop/test $ git submodule add ../remote/ submod2
Cloning into 'submod2'...
done.
bartb@EB-Latitude-E5450 ~/Desktop/test $ cd submod1
bartb@EB-Latitude-E5450 ~/Desktop/test/submod1 $ git log
commit 42d476962fc4e25c64ff2a807d2bf9b3e2ea31f8
Author: Bart Bogaerts <bart.bogaerts@cs.kuleuven.be>
Date: Tue Jun 21 08:56:05 2016 +0300
init commit
commit db1ba3bc4b02df4677f1197dc137ff36ddfeeb5f
Author: Bart Bogaerts <bart.bogaerts@cs.kuleuven.be>
Date: Tue Jun 21 08:55:52 2016 +0300
init commit
bartb@EB-Latitude-E5450 ~/Desktop/test/submod1 $ git checkout db1ba3bc4b02df4677f1197dc137ff36ddfeeb5f
Note: checking out 'db1ba3bc4b02df4677f1197dc137ff36ddfeeb5f'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at db1ba3b... init commit
bartb@EB-Latitude-E5450 ~/Desktop/test/submod1 $ cd ..
bartb@EB-Latitude-E5450 ~/Desktop/test $ git config -f .gitmodules submodule.submod2.branch master
bartb@EB-Latitude-E5450 ~/Desktop/test $ git commit -a -m "modules"
[master (root-commit) ea9e55f] modules
3 files changed, 9 insertions(+)
create mode 100644 .gitmodules
create mode 160000 submod1
create mode 160000 submod2
bartb@EB-Latitude-E5450 ~/Desktop/test $ git status
On branch master
nothing to commit, working directory clean
bartb@EB-Latitude-E5450 ~/Desktop/test $ git submodule update --recursive --remote
Submodule path 'submod1': checked out '42d476962fc4e25c64ff2a807d2bf9b3e2ea31f8'
bartb@EB-Latitude-E5450 ~/Desktop/test $ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: submod1 (new commits)
如你所见,在最新的git submodule update --remote submod1 在 master 上检出后,即使我从未为它配置过 master 分支。这就是我所说的“做错事”的意思
子子模块也会发生同样的事情:它们都是在 master 而不是在它们的特定提交时签出的。
这个“问题”其实是git submodule update --remote的预期。来自 git 文档:
This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch. The remote used is branch’s remote (branch.<name>.remote), defaulting to origin. The remote branch used defaults to master, but the branch name may be overridden by setting the submodule.<name>.branch option in either .gitmodules or .git/config (with .git/config taking precedence).
https://git-scm.com/docs/git-submodule
尤其是部分:
The remote branch used defaults to master
这是我想要避免的。
编辑:附加要求是:我不想对子模块或子模块进行任何修改(这些是联合项目)。
【问题讨论】:
-
你用的是什么git版本?
-
git 版本 2.6.0.rc3
-
2.9.0 版本是否仍然存在此问题?
-
对不起,我刚刚意识到我在另一台电脑上做了上述测试(应该检查那里安装的 git 版本)。
-
我目前无法访问我拥有适当权限的 PC。明天我会检查它是否仍然存在。
标签: git git-branch git-submodules