2012-04-16 132 views
171

我的項目strutureGit的子模塊更新遞歸

ProjectA 
-FrameworkA (submodule) 
--Twig (submodule of FrameworkA) 

如何更新子模塊遞歸?我已經嘗試過(上項目A根)

git submodule foreach git pull origin master 
or 
git submodule foreach --recursive git pull origin master 

一些git的命令,但不能拉枝條

的文件
+0

如何[混帳深(https://github.com/bluejamesbond/git-deep)? – bluejamesbond 2017-01-02 00:47:44

回答

374
git submodule update --recursive 

您也可能希望使用--init選項,這將使任何初始化未初始化的子模塊:

git submodule update --init --recursive 

注:在一些老版本的Git,如果你使用--init選項,可能不會更新已經初始化的子模塊。在這種情況下,您還應該運行不帶--init選項的命令。

+0

如何遞歸添加子模塊? 「git子模塊添加FrameworkA.git」只是拉取FrameworkA的文件。 – complez 2012-04-16 04:48:09

+1

你可以做一個「git submodule add blah」然後「git submodule update --init --recursive」。 – drewag 2012-04-16 13:37:58

+0

這是不是比我的方式不同? – 2013-09-26 13:30:21

18

我使用的方法是:

git submodule update --init --recursive 
git submodule foreach --recursive git fetch 
git submodule foreach git merge origin master 
+2

我改變了最後一行:'git submodule foreach git pull - 只支持原始主' – 2014-10-23 07:43:50

+1

我也會添加 - 遞歸到最後一行:「git submodule foreach --recursive git merge origin master」否則當它自己更新子模塊時,您可以獲得一個髒子模塊。 – 2015-10-02 18:54:47

+0

過去三個小時一直在尋找這個。謝謝你,先生。 要添加到這一點,你也可以使用犯這些命令,如: 'git的子模塊的foreach --recursive「git的承諾-a | :「'。無論結果如何,':'都會使其循環。見[鏈接](https://stackoverflow.com/questions/19728933/continue-looping-over-submodules-with-the-git-submodule-foreach-command-after)https://stackoverflow.com/questions/19728933 /繼續套住-過子模塊-與最GIT-子模塊-的foreach-命令後。 – 2017-05-23 02:10:18

12

,因爲它可能會發生,你的子模塊的默認分支是master(發生了很多我的情況),這是我的自動化全Git的子模塊升級:

git submodule init 
git submodule update 
git submodule foreach 'git fetch origin; git checkout $(git rev-parse --abbrev-ref HEAD); git reset --hard origin/$(git rev-parse --abbrev-ref HEAD); git submodule update --recursive; git clean -dfx' 
+0

我嘗試在我的[通用Makefile](https://github.com/Falkor/Makefiles/blob/devel/repo/Makefile)中添加這個命令,但我仍然陷入困境讓GNU Make * ignore *解釋$(...)序列,儘管它存在於簡單引號內。任何人有想法? – 2014-03-19 13:57:05

+0

你的命令是我需要的,謝謝你!但我得到:'進入「核心」 致命的:模棱兩可的說法「出身/ HEAD」:未知修訂或路徑不工作的tree.'其中'Core' 是子模塊 – Sanandrea 2017-05-03 13:01:28

+0

有用的信息,謝謝你的分享! – 2017-07-04 19:09:18

0

在最近的Git(我使用v2.15.1),以下將合併上游子模塊變成遞歸子模塊:

git submodule update --recursive --remote --merge 

您可以添加--init初始化任何未初始化的子模塊,並使用--rebase如果你想變基,而不是合併。

您需要事後提交更改:

git add . && git commit -m 'Update submodules to latest revisions'