Skip to content

Commit 05ea28d

Browse files
committed
层克隆
1 parent 94990ea commit 05ea28d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

docs/InfoTech/Git/commands/clone.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,42 @@
99
| 比如,要克隆 Ruby 语言的 Git 代码仓库 Grit,可以用下面的命令: | `git clone git://github.com/schacon/grit.git` | 执行该命令后,会在当前目录下创建一个名为grit的目录,其中包含一个 .git 的目录,用于保存下载下来的所有版本记录。 |
1010
| 如果要自己定义要新建的项目目录名称,可以在上面的命令末尾指定新的名字: | `git clone git://github.com/schacon/grit.git mygrit` | mygrit是指定的名字。 |
1111
| 克隆非22端口的项目 | `git clone ssh://git@<hostname>:<port>/<作者>/<项目名>.git` |hostname 可以是网址也可以是IP |
12+
13+
## 浅层克隆
14+
15+
在处理大型仓库或网络条件较差时,只获取最新一次提交可以显著提升克隆速度并减少磁盘占用。Git 提供了 **浅层克隆(Shallow Clone)** 功能来实现这一需求。
16+
17+
执行浅克隆命令 `git clone --depth 1 <仓库地址>`
18+
19+
指定分支(可选) 如果只需要某个分支的最新提交:`git clone --depth 1 --branch main <仓库地址>`
20+
21+
验证结果,执行:`git log --oneline`
22+
23+
### 注意事项
24+
25+
功能限制:浅克隆无法查看完整历史,git blame、rebase、跨分支切换等操作可能受限。
26+
27+
推送限制:部分 Git 服务端可能拒绝从浅克隆直接推送。
28+
29+
扩展历史:如需更多历史,可执行:
30+
31+
```bash
32+
git fetch --depth=<N> # 增加到最近 N 次提交
33+
git fetch --unshallow # 转换为完整克隆
34+
```
35+
36+
### 最佳实践
37+
38+
- 在 CI/CD、部署环境或仅需最新代码的场景中使用 --depth 1。
39+
40+
- 避免在浅克隆上进行长期开发,如需复杂操作请先转换为完整克隆。
41+
42+
- 可结合 `--filter=blob:none` 与稀疏检出(sparse checkout)进一步减少下载内容:
43+
44+
```bash
45+
git clone --depth 1 --filter=blob:none --sparse <仓库地址>
46+
cd 项目目录
47+
git sparse-checkout add path/to/folder
48+
```
49+
50+
这样,你就能快速获取所需代码,同时最大限度节省时间与资源。

0 commit comments

Comments
 (0)