在 Git 中使用子模块克隆远程仓库

Kevin Amayi 2023年1月30日 2022年4月20日
  1. 在 Git 中使用子模块克隆远程仓库
  2. 在 Git 中克隆之前创建一个子模块并推送到远程仓库
在 Git 中使用子模块克隆远程仓库

本文将讨论如何使用子模块克隆远程 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'

相关文章 - Git Clone