在使用git submodule 的時候,常常會遇到 執行完以下操作后發現 子倉庫的head 指針處于游離狀態
- git clone xxxxx.git
- git submodule update --init
然后切換到子倉庫,查看當前分支的狀態如下

原因是之前同事在子倉庫中修改并提交后,沒有在 父倉庫中更新子倉庫的最新提交記錄
正常情況下,修改子倉庫的內容并在子倉庫提交后會在父倉庫執行 git diff
會有如下輸出

解決git submodule head detached的方法:
重新建立submodule,加入時使用-b參數,使得母項目追蹤子項目的指定branch(否則默認不追蹤):
git submodule add -b branch> repository> [submodule-path>]
git submodule update --remote
簡單的一行命令遞歸修復所有子項目的detached head(其中默認都追蹤子項目的master branch):
git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
參考
Why is my Git Submodule HEAD detached from master?
Git submodules best practices
到此這篇關于詳解git submodule HEAD detached 的問題的文章就介紹到這了,更多相關git submodule HEAD detached內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解git submodule update獲取不到最新提交的代碼
- Git Submodule管理項目子模塊的使用
- 詳解git submodule使用以及注意事項
- git中submodule子模塊的添加、使用和刪除的示例代碼
- Git Submodule使用完整教程(小結)