我的情況:我有大量用于各種任務的計算機。我有大量的庫,每個庫都有自己的 git repo。
我的愿望:我希望能夠在任何計算機上修改其中一個庫,執行 git commit/push;然后轉到另一臺計算機,執行git pull,并更新所有庫。然后我修改其中一個庫,提交/推送,當我拿到下一臺計算機時,一切正常。
我目前的嘗試:我有一個頂級 git 專案,它將所有其他庫 repos 合并為子模塊。這包括一個 .gitmodules 檔案,該檔案通過使用指定每個模塊的作業分支
git config -f .gitmodules submodule.modulename.branch develop
我已經update = merge為每個模塊設定了。我已經submodule.recurse設定為 true,所以git pull在頂層對每個模塊做一些事情。
它是如何被打破的: 頭變得分離。我撰寫了一個腳本來決議.gitmodules檔案并執行checkout每個模塊的相應分支。我然后commit和push頂部模塊。每當我修改東西并嘗試拉動時,例如在另一臺機器上,頭部就會分離。如果我在開始修改之前沒有注意到頭部已分離,我必須在提交更改之前仔細整理殘骸。
在過去的十年里,實際上有 3.6k 的關于 git 分離頭的堆疊溢位問題,而且大多數似乎來自子模塊功能。我還沒有經歷所有這些,但我嘗試過的沒有奏效。
我忘記了為什么我拒絕了git-subtree,但git-subrepo一年多沒有接觸過,有 153 個問題和 25 個拉取請求待處理,所以我認為它已經死了。
有沒有人對此有有效的解決方案?
@vonC 接受的答案看起來不錯。
我可能可以稍微簡化一下,但是我的頂級專案的自述檔案現在說:
推薦結帳:
git clone --recursive --jobs=8 *mysuperproject_clone_url*
cd *mysuperproject*
git config alias.pullall 'submodule foreach git pull'
git config alias.statusall 'submodule foreach git status'
git config alias.switchall \
"submodule foreach --recursive 'git switch \$(git config -f \${toplevel}/.gitmodules submodule.\${sm_path}.branch)'"
git switchall
從存盤庫更新
git pullall
如果頭部脫落,請使用
git switchall
添加模塊
在以下示例中名為newmoduleworking on path 的模塊develop。
cd /path/to/mysuperproject
git submodule add [email protected]:myaccount/newmodule
git config -f .gitmodules submodule.newmodule.branch develop
git config -f .gitmodules submodule.newmodule.update merge
If the submodule is on the default master branch, you still have to config the branch.
If you switch a submodule to a different branch, then in the top level you must config again
git config -f .gitmodules submodule.newmodule.branch newbranch
And push both the submodule and the top level project.
On a different working directory (such as on a different machine), you must
cd /path/to/mysuperproject
git pull
git switchall
git pullall
uj5u.com熱心網友回復:
我之前提到過,git submodule update --remote --merge應該不會在分支之后分離子模塊的 HEAD。
我了解您已設定update = merge,但只是為了測驗,請嘗試完整的更新命令,看看這是否有效。
由于 HEAD 仍處于分離狀態,因此您需要添加(例如,添加到 git 別名腳本)命令
git submodule foreach --recursive git switch $(git config -f .gitmodules submodule.${sm_path}.branch)
我剛剛測驗了它:
首先,在 Git 存盤庫中,我檢查子模塊是否處于分離 HEAD 模式:
vonc@vclp MINGW64 ~/git/git (master)
$ git submodule update --init
Submodule 'sha1collisiondetection' (https://github.com/cr-marcstevens/sha1collisiondetection.git) registered for path 'sha1collisiondetection'
Cloning into 'C:/Users/vonc/git/git/sha1collisiondetection'...
Submodule path 'sha1collisiondetection': checked out '855827c583bc30645ba427885caa40c5b81764d2'
vonc@vclp MINGW64 ~/git/git/sha1collisiondetection (master)
$ git br
* (HEAD detached at 855827c)
master
然后我定義我的別名,用轉義$: \$。訪問檔案時
不需要。這就是目的。../.gitmodules$toplevel
vonc@vclp MINGW64 ~/git/git (master)
$ git config alias.switchall \
"submodule foreach --recursive 'git switch \$(git config -f \${toplevel}/.gitmodules submodule.\${sm_path}.branch)'"
最后一個考試:
vonc@vclp MINGW64 ~/git/git (master)
$ git switchall
Entering 'sha1collisiondetection'
Previous HEAD position was 855827c Detect endianess on HP-UX
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
vonc@vclp MINGW64 ~/git/git (tmp)
$ cd sha1collisiondetection/
vonc@vclp MINGW64 ~/git/git/sha1collisiondetection (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
vonc@vclp MINGW64 ~/git/git/sha1collisiondetection (master)
$ git branch
* master
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409115.html
標籤:
