2011-01-06 101 views
108

有沒有一種方法可以自動git submodule update(或最好git submodule update --init每當git pull做叫什麼名字?有沒有辦法讓git pull自動更新子模塊?

尋找一個混帳配置設置,或一個git的別名,以幫助這一點。

+3

相關:http://stackoverflow.com/questions/1899792/why-is-git-submodule-update-not-automatic – philfreo 2011-01-06 03:40:26

+0

爲什麼git別名比shell別名更可取? – wnoise 2011-01-06 03:57:09

+14

git別名很好,因爲它將命令封裝在「git」命名空間中。你可能會問,爲什麼所有的git命令都以「git」開頭,而不是自己的名字。 – 2011-01-06 04:00:04

回答

99

git config --global alias.pullall '!git pull && git submodule update --init --recursive'

如果你想參數傳遞給git pull,然後用這個代替:

git config --global alias.pullall '!f(){ git pull "[email protected]" && git submodule update --init --recursive; }; f' 
+3

如果你想在你使用的所有git倉庫中使用這個別名,請記得使用「git config --global」 – yoyo 2015-10-08 23:47:32

8

一個別名,如Kevin巴拉德,是一個非常好的解決方案。只是爲了拋出另一個選項,你也可以使用一個簡單運行git submodule update [--init]的後合併鉤子。

39

從Git 1.7.5開始,它應該默認自動更新子模塊,就像你想要的那樣。

[編輯:每評論:新的1.7.5行爲是自動獲取最新提交的子模塊,但不(在git submodule update意義上)更新他們。所以這個答案中的信息與背景有關,但並不是完整的答案。您仍然需要一個別名來在一個命令中提取和更新子模塊。]

「按需」的默認行爲是在您獲取更新子模塊提交的提交時更新子模塊,而此提交不是已經位於你的本地克隆中。
您也可以在每次抓取時更新它,或從不(我假設1.7.5之前的行爲)。
用於更改此行爲的配置選項是fetch.recurseSubmodules

該選項可以設置爲布爾值,也可以設置爲on-demand
將其設置爲布爾值會將fetchpull的行爲更改爲在設置爲true時無條件遞歸到子模塊中,或者在設置爲false時將其無法遞歸。

當設置爲on-demand(默認值),fetchpull當其子項目會檢索一個提交,更新子模塊的參考只會遞歸到一個人口稠密的子模塊。

參見:

以獲取更多信息。

git fetch --recurse-submodules[=yes|on-demand|no] 
+27

小心:如下面的答案所解釋的那樣,這隻會自動獲取更改,您仍然需要執行子模塊更新 - 所以別名的答案是正確的。 – Artem 2011-11-03 16:02:27

+4

@Artem是正確的。這個答案雖然有用,但並不涉及整個問題。這個設置只是執行'git fetch',而不是'git submodule update'。 – 2014-04-26 13:16:49

+2

這個答案具有很高的欺騙性。即使與'git pull'一起使用,而不是'git fetch',這個選項只會使* fetching *遞歸。它根本不會改變在子模塊中籤出的提交。所以'git submodule update'仍然是必需的,正如@Artem所指出的那樣。 – 2015-02-19 10:12:30

3

您可以爲自動處理子模塊更新的git命令創建別名。將以下內容添加到您的。bashrc

# make git submodules usable 
# This overwrites the 'git' command with modifications where necessary, and 
# calls the original otherwise 
git() { 
    if [[ [email protected] == clone* ]]; then 
     gitargs=$(echo "[email protected]" | cut -c6-) 
     command git clone --recursive $gitargs 
    elif [[ [email protected] == pull* ]]; then 
     command git "[email protected]" && git submodule update --init --recursive 
    elif [[ [email protected] == checkout* ]]; then 
     command git "[email protected]" && git submodule update --init --recursive 
    else 
     command git "[email protected]" 
    fi 
} 
+1

而不是git的別名,你可以通過alias命令添加別名來通過git添加別名,或者在你的路徑中創建以git-(git-bettermodule) – idbrii 2016-10-06 17:17:11

21

我很驚訝沒有人提到使用git hooks來做到這一點!

剛剛任命post-checkoutpost-merge文件添加到您.git/hooks目錄的相關信息庫,並把下列每個人:

#!/bin/sh 
git submodule update --init --recursive 

既然你specfically問一個別名,假設你想擁有這對於許多存儲庫,您可以創建一個別名,將它們添加到存儲庫的.git/hooks

+0

開頭的命令有沒有辦法讓這是一個全局設置?還是在檢出存儲庫時自動獲得的一個? – 2016-07-13 09:00:21

+0

git 2.9的最新版本[爲hooks目錄添加了一個名爲'core.hooksPath'的設置](https://github.com/blog/2188-git-2-9-has-been-released# git-tidbits-gitbits-tidgits),更多詳細信息請參閱「git-config」的文檔。 – taleinat 2016-07-13 16:29:53

+0

至於退房時自動收到的東西,我搜索了但沒有找到任何類型的東西。一位消息來源提到,這故意不支持安全問題,因爲它可以很容易地用於在客戶機上運行任意代碼。 – taleinat 2016-07-13 16:32:35

相關問題