簡體   English   中英

沒有分離頭的Git子模塊?

[英]Git submodules without detached head?

也許我沒有故意使用git-submodules,但如果有任何其他git功能滿足我的情況,那么很高興找到它。

在克隆存儲庫之后,我希望子模塊在master分支中,我永遠不會在chef-starter子模塊中存儲任何其他文件。

git clone git@github.com:holms/vagrant-starter.git
git submodule update --init
cd chef-starter
git branch
   * (detached from 0b6a803)
   master

我確實理解這是一個很好的功能,分別跟蹤該子模塊目錄樹,但它不是我的情況。 在主存儲庫克隆之后,我只想將該子模塊克隆到最新的主階段而不需要任何額外的麻煩。

我怎樣才能做到這一點?

子模塊有一個分離的頭,因為子模塊意味着“從子模塊的存儲庫中檢出特定的提交”。 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-subtree而不是子模塊更容易:

# 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回購信息移植到您的vagrant-starter repo中的chef-starter/目錄中。 從那時起,如果您選擇編輯子樹中的文件,則不必執行任何特殊操作。 例如,您不必在克隆后運行git submodule update

為了確保我的子模塊保持同步和未分離,我在批處理文件中有以下內容。 它依賴於采用同步到子模塊主分支的策略 - 對我來說這不是問題,我只是分叉子模塊並確保它有一個主模塊。 我發現用這種方法跟蹤事情要容易得多。

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

使用以下:

1)添加子模塊時使用-b鍵設置其分支:

git submodule add -b master git@github.com:Bla-bla/submodule.git sm_dir

2)更新子模塊時使用--merge param將更新合並到CURRENT子模塊的分支(最有可能是master ):

git submodule update --merge

就這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM