2017-08-17 76 views
0

我有兩個分支,主BR1和BR2。現在,我做了以下git狀態顯示令人困惑的結果

git checkout BR1 
git submodule add <submodule1_repo_url> path/to/submodule1 
git submodule add <submodule2_repo_url> path/to/submodule2 
git add path/to/submodule1 
git add path/to/submodule2 
git commit -m "Some commit msg" 

當我做了git status現在,我看到我的改變是承諾並沒有什麼承諾。

現在,當我切換到BR2並執行git status時,我看到path/to/submodule1path/to/submodule2顯示爲未跟蹤文件。重點是我在BR1中添加了這些子模塊。爲什麼它們在BR2中顯示爲未被跟蹤,我如何確保它們在BR2中未被追蹤,因爲我不想在BR2下提交這些子模塊。

任何幫助,將不勝感激。

編輯:中git status上BR1

On branch BR1 
nothing to commit, working tree clean 

git status輸出 輸出上BR2

On branch BR2 
Your branch is up-to-date with 'origin/master'. 
Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

    path/to/submodule1/ 
    path/to/submodule2/ 

nothing added to commit but untracked files present (use "git add" to track) 
+0

請顯示'git status'的**確切**輸出以及您的口頭解釋。 –

+0

在提交了BR1上的更改後,在兩個分支上添加了'git status'的確切輸出。 – thisisshantzz

回答

2

git當你切換分支不會刪除該子模塊目錄的內容。它們表現爲未跟蹤,因爲您沒有將它們添加到BR2。你可以放心地忽略它們。

你只能看到頂層的子模塊目錄而不是它的內容,因爲它們是git存儲庫(它們包含.git目錄),所以git忽略了他們的內容,理論上你不會直接將它添加到父目錄儲存庫。

+0

因此,在.gitignore中添加一個條目將是安全的事情嗎? – thisisshantzz

1

如果您有幾個分支具有不同的子模塊集,則可能有兩種方法來處理它們。

變體1:一個post-checkout掛鉤處理子模塊。像這樣的東西(我真的有這樣的鉤子):

#!/bin/sh 

# post-checkout hook that switches submodules 

prev_HEAD="$1" 
new_HEAD="$2" 
new_branch="$3" 

if [ "$new_branch" = 1 ]; then 
    if ! cat .gitmodules | grep -Fq SUBMODULE1; then 
     rm -rf SUBMODULE1 SUBMODULE2 
    fi 

    git submodule update 
fi 

exit 0 

變體2:工作樹。我的git有點舊,所以我不使用git worktree。我剛剛創建了克隆,簽出了長壽命的分支,並且從不在克隆中檢出不同的分支。

+0

不會在BR2的gitignore中添加條目比運行您製作的結賬後掛鉤更好嗎? – thisisshantzz

+0

在我的情況下,這並不是因爲我真的有3個不同子模塊的分支。所以後來我決定去找工作和長壽的分支。如果你滿足於忽略分支中的子模塊 - 繼續,爲什麼不呢? – phd