使用 git 个子模块创建多个远程分支
Creating multiple remote branches with git submodules
我是第一次使用 git 子模块。努力理解如何全面创建分支并将它们添加到所有远程仓库中。
目前我的文件结构如下所示:
-parent_repo
|
|_ submodule_1
|_ submodule_2
|_ submodule_3
|_ submodule_4
|_ submodule_5
|_ submodule_6
|_ submodule_7
如果我在父仓库上创建一个分支:
(master) $ git checkout -b feature/my_feature
(feature/my_feature) $ git commit -m "created my_feature"
(feature/my_feature) $ git push -u origin feature/my_feature
我想在包括父模块在内的所有子模块中创建一个分支。之后,所有分支都被远程推送到每个子模块及其相关的仓库。
尝试了以下步骤:
$ git submodule foreach -b branch_name
$ git push --recurse-submodules=on-demand
$ git submodule foreach "(git checkout branch_name; git pull)&"
..只是失败了。找不到第一个命令。
..如果我这样做:
$ git config -f .gitmodules submodule.submodule_1.branch branch_name
$ git submodule update --remote
git returns:
fatal: Needed a single revision
Unable to find current origin/branch_name revision in submodule path 'submodule_1'
For example, let’s say we want to start a new feature or do a bugfix and we have work going on in several submodules.
We can easily stash all the work in all our submodules:
$ git submodule foreach 'git stash'
Entering 'CryptoLibrary'
No local changes to save
Entering 'DbConnector'
Saved working directory and index state WIP on stable: 82d2ad3 Merge from origin/stable
HEAD is now at 82d2ad3 Merge from origin/stable
Then we can create a new branch and switch to it in all our submodules:
$ git submodule foreach 'git checkout -b featureA'
Entering 'CryptoLibrary'
Switched to a new branch 'featureA'
Entering 'DbConnector'
Switched to a new branch 'featureA'
那么你仍然需要在父仓库中创建相同的分支,添加、提交和推送,因为所有子模块仓库都将发生变化。
但请记住:这些是不同的分支(即使它们具有相同的名称),每个分支都特定于自己的存储库(父存储库或子模块存储库)。
I would like to create a branch across all submodules including the parent
这将在 Git 2.36(2022 年第 2 季度)正式支持(首先在 实验模式 中)。
"git branch
"(man) 学习了 --recurse-submodules
选项。
参见 commit 09e0be1 (31 Jan 2022) by Junio C Hamano (gitster
)。
参见 commit 679e369, commit 961b130, commit 6e0a2ca, commit 3f3e760, commit bc0893c, commit e89f151 (28 Jan 2022) by Glen Choo (chooglen
)。
(由 Junio C Hamano -- gitster
-- in commit 5cc9522 合并,2022 年 2 月 18 日)
branch
: add --recurse-submodules
option for branch creation
Helped-by: Jonathan Tan
Signed-off-by: Glen Choo
Reviewed-by: Jonathan Tan
To improve the submodules UX, we would like to teach Git to handle branches in submodules.
Start this process by teaching "git branch
"(man) the --recurse-submodules
option so that git branch --recurse-submodules
(man) topic will create the topic
branch in the superproject and its submodules.
Although this commit does not introduce breaking changes, it does not work well with existing --recurse-submodules
commands becausegit branch --recurse-submodules
" writes to the submodule ref store, but most commands only consider the superproject gitlink
and ignore the submodule ref store.
For example, "git checkout --recurse-submodules
"(man) will check out the commits in the superproject gitlinks
(and put the submodules in detached HEAD) instead of checking out the submodule branches.
Because of this, this commit introduces a new configuration value, submodule.propagateBranches
.
The plan is for Git commands to prioritize submodule ref store information over superproject gitlinks
if this value is true.
Because "git branch --recurse-submodules
" writes to submodule ref stores, for the sake of clarity, it will not function unless this configuration value is set.
This commit also includes changes that support working with submodules from a superproject commit because "branch --recurse-submodules
" (and future commands) need to read .gitmodules
and gitlinks
from the superproject commit, but submodules are typically read from the filesystem's .gitmodules
and the index's gitlinks.
These changes are:
- add a
submodules_of_tree()
helper that gives the relevant information of an in-tree submodule (e.g. path and oid) and initializes the repository
- add
is_tree_submodule_active()
by adding a treeish_name
parameter to is_submodule_active()
- add the "submoduleNotUpdated" advice to advise users to update the submodules in their trees
Incidentally, fix an incorrect usage string that combined the 'list' usage of git branch
(-l) with the 'create' usage; this string has been incorrect since its inception, a8dfd5e ("Make builtin-branch.c
use parse_options
.", 2007-10-07, Git v1.5.4-rc0 -- merge).
git config
现在包含在其 man page 中:
submodulesNotUpdated
Advice shown when a user runs a submodule command that fails
because git submodule update --init
was not run.
git config
现在包含在其 man page 中:
submodule.recurse
:
A boolean indicating if commands should enable the --recurse-submodules
option by default.
Defaults to false.
When set to true, it can be deactivated via the
--no-recurse-submodules
option. Note that some Git commands
lacking this option may call some of the above commands affected by
submodule.recurse
; for instance git remote update
will call
git fetch
but does not have a --no-recurse-submodules
option.
For these commands a workaround is to temporarily change the
configuration value by using git -c submodule.recurse=0
.
The following list shows the commands that accept
--recurse-submodules
and whether they are supported by this
setting.
checkout
, fetch
, grep
, pull
, push
, read-tree
,
reset
, restore
and switch
are always supported.
clone
and ls-files
are not supported.
branch
is supported only if submodule.propagateBranches
is
enabled
submodule.propagateBranches
[EXPERIMENTAL] A boolean that enables branching support when
using --recurse-submodules
or submodule.recurse=true
.
Enabling this will allow certain commands to accept
--recurse-submodules
and certain commands that already accept
--recurse-submodules
will now consider branches.
git branch
现在包含在其 man page 中:
'git branch' [--track[=(direct|inherit)] | --no-track] [-f]
[--recurse-submodules] <branchname> [<start-point>]
git branch
现在包含在其 man page 中:
--recurse-submodules
THIS OPTION IS EXPERIMENTAL! Causes the current command to
recurse into submodules if submodule.propagateBranches
is
enabled. See submodule.propagateBranches
in
git config
.
Currently, only branch creation is supported.
When used in branch creation, a new branch <branchname>
will be created
in the superproject and all of the submodules in the superproject's
.
In submodules, the branch will point to the submodule
commit in the superproject's <start-point>
but the branch's tracking
information will be set up based on the submodule's branches and remotes
e.g. git branch --recurse-submodules topic origin/main
will create the
submodule branch "topic
" that points to the submodule commit in the
superproject's "origin/main
", but tracks the submodule's "origin/main
".
我是第一次使用 git 子模块。努力理解如何全面创建分支并将它们添加到所有远程仓库中。
目前我的文件结构如下所示:
-parent_repo
|
|_ submodule_1
|_ submodule_2
|_ submodule_3
|_ submodule_4
|_ submodule_5
|_ submodule_6
|_ submodule_7
如果我在父仓库上创建一个分支:
(master) $ git checkout -b feature/my_feature
(feature/my_feature) $ git commit -m "created my_feature"
(feature/my_feature) $ git push -u origin feature/my_feature
我想在包括父模块在内的所有子模块中创建一个分支。之后,所有分支都被远程推送到每个子模块及其相关的仓库。
尝试了以下步骤:
$ git submodule foreach -b branch_name
$ git push --recurse-submodules=on-demand
$ git submodule foreach "(git checkout branch_name; git pull)&"
..只是失败了。找不到第一个命令。
..如果我这样做:
$ git config -f .gitmodules submodule.submodule_1.branch branch_name
$ git submodule update --remote
git returns:
fatal: Needed a single revision
Unable to find current origin/branch_name revision in submodule path 'submodule_1'
For example, let’s say we want to start a new feature or do a bugfix and we have work going on in several submodules.
We can easily stash all the work in all our submodules:
$ git submodule foreach 'git stash'
Entering 'CryptoLibrary'
No local changes to save
Entering 'DbConnector'
Saved working directory and index state WIP on stable: 82d2ad3 Merge from origin/stable
HEAD is now at 82d2ad3 Merge from origin/stable
Then we can create a new branch and switch to it in all our submodules:
$ git submodule foreach 'git checkout -b featureA'
Entering 'CryptoLibrary'
Switched to a new branch 'featureA'
Entering 'DbConnector'
Switched to a new branch 'featureA'
那么你仍然需要在父仓库中创建相同的分支,添加、提交和推送,因为所有子模块仓库都将发生变化。
但请记住:这些是不同的分支(即使它们具有相同的名称),每个分支都特定于自己的存储库(父存储库或子模块存储库)。
I would like to create a branch across all submodules including the parent
这将在 Git 2.36(2022 年第 2 季度)正式支持(首先在 实验模式 中)。
"git branch
"(man) 学习了 --recurse-submodules
选项。
参见 commit 09e0be1 (31 Jan 2022) by Junio C Hamano (gitster
)。
参见 commit 679e369, commit 961b130, commit 6e0a2ca, commit 3f3e760, commit bc0893c, commit e89f151 (28 Jan 2022) by Glen Choo (chooglen
)。
(由 Junio C Hamano -- gitster
-- in commit 5cc9522 合并,2022 年 2 月 18 日)
branch
: add--recurse-submodules
option for branch creationHelped-by: Jonathan Tan
Signed-off-by: Glen Choo
Reviewed-by: Jonathan Tan
To improve the submodules UX, we would like to teach Git to handle branches in submodules.
Start this process by teaching "git branch
"(man) the--recurse-submodules
option so thatgit branch --recurse-submodules
(man) topic will create thetopic
branch in the superproject and its submodules.Although this commit does not introduce breaking changes, it does not work well with existing
--recurse-submodules
commands becausegit branch --recurse-submodules
" writes to the submodule ref store, but most commands only consider the superprojectgitlink
and ignore the submodule ref store.For example, "
git checkout --recurse-submodules
"(man) will check out the commits in the superprojectgitlinks
(and put the submodules in detached HEAD) instead of checking out the submodule branches.Because of this, this commit introduces a new configuration value,
submodule.propagateBranches
.
The plan is for Git commands to prioritize submodule ref store information over superprojectgitlinks
if this value is true.Because "
git branch --recurse-submodules
" writes to submodule ref stores, for the sake of clarity, it will not function unless this configuration value is set.This commit also includes changes that support working with submodules from a superproject commit because "
branch --recurse-submodules
" (and future commands) need to read.gitmodules
andgitlinks
from the superproject commit, but submodules are typically read from the filesystem's.gitmodules
and the index's gitlinks.These changes are:
- add a
submodules_of_tree()
helper that gives the relevant information of an in-tree submodule (e.g. path and oid) and initializes the repository- add
is_tree_submodule_active()
by adding atreeish_name
parameter tois_submodule_active()
- add the "submoduleNotUpdated" advice to advise users to update the submodules in their trees
Incidentally, fix an incorrect usage string that combined the 'list' usage of
git branch
(-l) with the 'create' usage; this string has been incorrect since its inception, a8dfd5e ("Makebuiltin-branch.c
useparse_options
.", 2007-10-07, Git v1.5.4-rc0 -- merge).
git config
现在包含在其 man page 中:
submodulesNotUpdated
Advice shown when a user runs a submodule command that fails because
git submodule update --init
was not run.
git config
现在包含在其 man page 中:
submodule.recurse
:A boolean indicating if commands should enable the
--recurse-submodules
option by default.
Defaults to false.When set to true, it can be deactivated via the
--no-recurse-submodules
option. Note that some Git commands lacking this option may call some of the above commands affected bysubmodule.recurse
; for instancegit remote update
will callgit fetch
but does not have a--no-recurse-submodules
option. For these commands a workaround is to temporarily change the configuration value by usinggit -c submodule.recurse=0
.The following list shows the commands that accept
--recurse-submodules
and whether they are supported by this setting.
checkout
,fetch
,grep
,pull
,push
,read-tree
,reset
,restore
andswitch
are always supported.clone
andls-files
are not supported.branch
is supported only ifsubmodule.propagateBranches
is enabled
submodule.propagateBranches
[EXPERIMENTAL] A boolean that enables branching support when using
--recurse-submodules
orsubmodule.recurse=true
. Enabling this will allow certain commands to accept--recurse-submodules
and certain commands that already accept--recurse-submodules
will now consider branches.
git branch
现在包含在其 man page 中:
'git branch' [--track[=(direct|inherit)] | --no-track] [-f]
[--recurse-submodules] <branchname> [<start-point>]
git branch
现在包含在其 man page 中:
--recurse-submodules
THIS OPTION IS EXPERIMENTAL! Causes the current command to recurse into submodules if
submodule.propagateBranches
is enabled. Seesubmodule.propagateBranches
ingit config
.
Currently, only branch creation is supported.When used in branch creation, a new branch
<branchname>
will be created in the superproject and all of the submodules in the superproject's .In submodules, the branch will point to the submodule commit in the superproject's
<start-point>
but the branch's tracking information will be set up based on the submodule's branches and remotes e.g.git branch --recurse-submodules topic origin/main
will create the submodule branch "topic
" that points to the submodule commit in the superproject's "origin/main
", but tracks the submodule's "origin/main
".