2017-02-16 76 views
3

我有一個父項目具有以下.gitmodules文件設置git的子模塊,而無需創建提交

[submodule "src/redux"] 
    path = src/redux 
    url = [email protected]:username/sub-package.git 
    branch = master 

下面是我使用克隆父項目,以確保我有子模塊設置的步驟。

git clone [email protected]:username/parent-project.git 
git submodule init 
git submodule update 

之後,我在子項目中的位置的子模塊的內容。但是,HEAD分離在子模塊中。首先表明我沒有做正確的事。由於父項目的.gitmodules文件指向主分支,因此我期望它位於主分支上。

enter image description here

然後我檢查了主機和設立子模塊的主分支用於跟蹤遠程主分支。

git checkout master 
git branch -u origin/master master 

當我退出到父項目並運行git status時,我有一個未提交的子模塊更改。

enter image description here

我覺得我不應該當我剛剛建立的一切,起初這種變化。如果我這樣做了,克隆該項目的所有開發人員在設置本地環境時也會有自己獨特的初始提交。我究竟做錯了什麼?

+0

的可能的複製[git的子模塊修改後的文件狀態(http://stackoverflow.com/questions/6006494/git-submodule-modified-files-status) – smarber

+0

@smarber,做一些回答但我仍然不明白'.gitsubmodules'中的'branch = master'行對於 – David

回答

0

子模塊是有點棘手的工作;如果你有他們,你需要確保你的團隊中的每個開發人員都瞭解Git模型它如何處理子模塊。

將子模塊添加到回購站時,子模塊的內容永遠不會添加到回購站本身;只有父回購記錄關於子模塊的事情是它對應的提交散列。如果您運行git diff -- src/redux,則可以看到此信息。

這意味着當你git submodule init,你正在更新子模塊的工作樹,以反映存儲在索引中的散列;從git-submodule(1)

init 
     Initialize the submodules recorded in the index (which were added 
     and committed elsewhere) by copying submodule names and urls from 
     .gitmodules to .git/config. Optional <path> arguments limit which 
     submodules will be initialized. It will also copy the value of 
     submodule.$name.update into .git/config. The key used in 
     .git/config is submodule.$name.url. This command does not alter 
     existing information in .git/config. You can then customize the 
     submodule clone URLs in .git/config for your local setup and 
     proceed to git submodule update; you can also just use git 
     submodule update --init without the explicit init step if you do 
     not intend to customize any submodule locations. 

.gitmodules存儲關於子模塊配置的其他信息,並且具體地submodule.src/redux.branch用於當你更新子模塊,即指向它在一個更近的提交;也git-submodule(1)

--remote 
     This option is only valid for the update command. Instead of using 
     the superproject’s recorded SHA-1 to update the submodule, use the 
     status of the submodule’s remote-tracking branch. The remote used 
     is branch’s remote (branch.<name>.remote), defaulting to origin. 
     The remote branch used defaults to master, but the branch name may 
     be overridden by setting the submodule.<name>.branch option in 
     either .gitmodules or .git/config (with .git/config taking 
     precedence). 
+0

有什麼好處如果在子模塊中創建並切換到特性分支然後返回到子模塊並執行'git子模塊更新'和'.gitmodules'文件指定'branch = master'?這是否拉大師承諾你的功能分支? – David

+0

再次,從'git-submodule(1)':「'update':更新已註冊的子模塊,以匹配超級項目期望通過克隆缺少的子模塊和更新子模塊的工作樹。根據命令行選項和子模塊的值,「更新」可以通過多種方式完成。 .update配置變量「。 – Pockets