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子模块更新命令行中传递——remote选项时,git将以分离HEAD模式(如果选择默认的——checkout行为)检出所选远程分支的最新提交。
You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules. The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote. If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.
毫无疑问,在git子模块update命令执行克隆阶段之后,它最终将尝试检出之前为子模块设置的远程分支的最新提交,如果这不是主分支,那么它就不是本地浅克隆的一部分,因此它将失败
致命的:需要一次修改 无法在子模块路径“mySubmodule”中找到当前的origin/NotThePrimaryBranch修订
其他回答
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子模块
使用下面的命令
添加子模块(分支-分支)
git submodule add -b stage git@github.optum.com:orgname/${reponame}.git
更新子模块(分支-分支)
######Clone########
> git clone -b master --single-branch --recurse-submodules git@github.com:orgname/project.git
or
> git clone -b stage --single-branch --recurse-submodules git@github.com:orgname/project.git
######Update#######
> git submodule update --remote (only for remote branch -ie master)
or
> git submodule update --recursive --remote
> git submodule update --init --recursive (for remaining branchs)
if you get fatal need single revision error then use below commands:-
before:-
*** stage
remotes/origin/stage**
git branch -a
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/{branch name}
ex:- git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/stage
after:-
*** stage
remotes/origin/HEAD -> origin/stage
remotes/origin/stage**
then
> git reset --hard
#hard reset revisions#
> git submodule foreach git reset --hard origin/develop
> git submodule foreach git pull origin develop
在提交和推送相应的.. 现在子模块将得到更新。
一个我如何使用Git子模块的例子。
创建一个新的存储库 然后将另一个存储库克隆为子模块 然后让该子模块使用一个名为V3.1.2的标记 然后我们承诺。
它看起来有点像这样:
git init
vi README
git add README
git commit
git submodule add git://github.com/XXXXX/xxx.yyyy.git stm32_std_lib
git status
git submodule init
git submodule update
cd stm32_std_lib/
git reset --hard V3.1.2
cd ..
git commit -a
git submodule status
也许它有帮助(即使我使用标记而不是分支)?
为了切换子模块的分支(假设你已经将子模块作为存储库的一部分):
CD到包含子模块的存储库的根目录 打开.gitmodules进行编辑 在path =…url =…对于每个子模块,它说branch = your-branch;保存文件。gitmodules。 然后在不改变目录的情况下执行$ git子模块update——remote
...对于这样修改的每个子模块,这应该拉入指定分支上的最新提交。
现有的答案忽略了第二步,并且有过多的细节。
切换一个现有的子模块来跟踪一个新的远程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:更新工作副本中签出的子模块。 提交更改。