在 Git 中使用子模組克隆遠端倉庫
Kevin Amayi
2024年2月15日
本文將討論如何使用子模組克隆遠端 Git 倉庫。我們還將建立一個子模組並將其推送到遠端倉庫,然後再克隆它。
在 Git 中使用子模組克隆遠端倉庫
我們使用以下命令將我們的倉庫與子模組一起克隆。
git clone --recurse-submodules -j8 git@github.com:KEVINAMAYI/AkanNameGenerator.git
輸出:
Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.86 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule 'testfolder' (git@github.com:KEVINAMAYI/AkanNameGenerator.git) registered for path 'testfolder'
Cloning into '/home/kevin/tqt/AkanNameGenerator/testfolder'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.55 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule path 'testfolder': checked out '3300a2aa47ef2c490c19541c6907117511eabe08'
在 Git 中克隆之前建立一個子模組並推送到遠端倉庫
在克隆倉庫之前,我們將首先將名為 testfolder
的子模組新增到已經存在的本地倉庫中,然後將更改推送到遠端倉庫。
<!-- this commands intializes a submodule with the contents of a remote repo-->
git submodule add <your remote repo url> <name of submodule>
git submodule add git@github.com:KEVINAMAYI/AkanNameGenerator.git testfolder
輸出:
Cloning into '/home/kevin/tst/AkanNameGenerator/testfolder'...
remote: Enumerating objects: 105, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 105 (delta 3), reused 0 (delta 0), pack-reused 94
Receiving objects: 100% (105/105), 2.38 MiB | 2.06 MiB/s, done.
Resolving deltas: 100% (28/28), done.
接下來,我們檢查我們的新檔案。我們應該在列表中看到一個額外的 testfolder
。
ls
輸出:
css images index.html js LICENSE README.md testfolder vendor
然後,我們將提交我們剛剛所做的更改。
git commit -m "Added the submodule to the project."
輸出:
"Added the submodule to the project."
[main 500a12a] Added the submodule to the project.
2 files changed, 4 insertions(+)
create mode 100644 .gitmodules
create mode 160000 testfolder
我們將使用此命令將更改推送到遠端倉庫。
git push
輸出:
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 429 bytes | 429.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:KEVINAMAYI/AkanNameGenerator.git
3300a2a..500a12a main -> main
現在我們的遠端倉庫有子模組 testfolder
。
我們將克隆我們的倉庫和子模組。
git clone --recurse-submodules -j8 git@github.com:KEVINAMAYI/AkanNameGenerator.git
輸出:
Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.86 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule 'testfolder' (git@github.com:KEVINAMAYI/AkanNameGenerator.git) registered for path 'testfolder'
Cloning into '/home/kevin/tqt/AkanNameGenerator/testfolder'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.55 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule path 'testfolder': checked out '3300a2aa47ef2c490c19541c6907117511eabe08'