2014-02-24 50 views
3

也許我不是故意使用git-submodules,但如果有任何其他符合我的情況的git功能,將很高興找到它。沒有分離頭的Git子模塊?

克隆存儲庫後,我想子模塊在主分支,我永遠不會存儲廚師啓動子模塊內的任何額外的文件。

git clone [email protected]:holms/vagrant-starter.git 
git submodule update --init 
cd chef-starter 
git branch 
    * (detached from 0b6a803) 
    master 

我明白,這是一個很好的功能,分別跟蹤該子模塊目錄樹,但它不是我的情況。在主版本庫克隆之後,我只想將該子模塊克隆到最新的主控階段,而不需要大驚小怪。

我怎樣才能做到這一點?

+0

'git clone --recursive URL dir; cd dir; git submodule foreach'git checkout origin/master || :'' –

+0

好的,當你在repo目錄上執行git status的時候,你會在執行最後一次簽出命令後看到「等待提交」。這是我不想要的。 – holms

回答

7

子模塊具有分離頭,因爲子模塊意味着「檢查子模塊存儲庫中的特定提交」。 master分支可能已經向前移動(它可能指向從落實0b6a803下降的提交),所以Git檢出特定修訂而不是檢出分支。

git-submodule可以選擇記錄分支名稱。當你這樣做,你可以使用git submodule update --remote與分支更新子模塊:

# Add the submodule with the "master" branch referenced 
git submodule add -b master https://github.com/holms/chef-starter.git chef-starter 

# When you want to update the submodule: 
git submodule update --remote 

# You'll still need to commit the submodule if you want future checkouts to use the new revision 
git add chef-starter 
git commit -m "Update chef-starter submodule" 

你還是輔助模塊獲得一個分離的頭,當你開始檢查出不同版本的超級項目(vagarant-starter)的,但至少現在更容易將子模塊從遠程更新到最新版本。

您可能會發現它更容易使用git的子樹,而不是子模塊:

# First, get rid of the submodule 
git submodule deinit chef-starter # If you have Git 1.8.3 or later, otherwise edit .gitmodules 
git rm chef-starter    # Remove the directory 
git commit -m "Removed chef-starter submodule in preparation to add as a subtree" 

# Now add the subtree 
git subtree add -P chef-starter https://github.com/holms/chef-starter master --squash 

該移植物chef-starter回購進入chef-starter/目錄在vagrant-starter回購。從那時起,如果您選擇編輯子樹中的文件,則不必做任何特別的事情。例如,克隆後不必運行git submodule update

+0

好吧,它確實適合我的第一個用例。我不必在回購中存儲「廚師首發」目錄,這很酷。雖然,我想在'vagrant-starter'回購庫裏面有一個'myrepo'子目錄,它有自己的git倉庫,對於'vagrant-starter'完全看不見。這可能嗎? – holms

+0

我剛剛通過忽略'chef-starter'目錄,並在我的生成文件中使用簡單的'git clone'而結束了使用。 – holms

1

爲了確保我的子模塊保持同步並解除綁定,我在批處理文件中包含以下內容。它依賴於採用同步到子模塊主分支的策略 - 對我來說這不是問題,我只需將子模塊分叉並確保它具有主分支。我發現用這種方法跟蹤事情要容易得多。

git pull 
git submodule sync # Ensure the submodule points to the right place 
git submodule update # Update the submodule 
git submodule foreach git checkout master # Ensure subs are on master branch 
git submodule foreach git pull origin master # Pull the latest master 
+0

作爲更新,我只想提及我更喜歡子樹合併作爲子模塊上的策略天: https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/ – cmaughan

0

使用下列內容:

1)當添加子模塊使用-b鍵設置分支機構:

git submodule add -b master [email protected]:Bla-bla/submodule.git sm_dir 

2)當更新子模塊使用--merge參數去合併更新CURRENT子模塊的分支(最有可能這將是master):

git submodule update --merge 

這一切