Skip to content

克隆仓库

概述

克隆(Clone)是将远程仓库完整复制到本地的操作。它会复制仓库的所有文件、分支、标签和提交历史,是参与现有项目的第一步。

git clone 基本用法

基本语法

bash
git clone <repository-url>

克隆 HTTPS 仓库

bash
git clone https://github.com/user/repo.git

克隆 SSH 仓库

bash
git clone git@github.com:user/repo.git

克隆到指定目录

bash
git clone <repository-url> <directory-name>

示例:

bash
git clone https://github.com/user/repo.git my-project

克隆过程解析

克隆时会自动:

  1. 创建本地仓库
  2. 添加远程仓库 origin
  3. 下载所有分支和标签
  4. 检出默认分支(通常是 main 或 master)
bash
git clone https://github.com/user/repo.git
cd repo
git remote -v

输出:

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

克隆指定分支

克隆单个分支

bash
git clone -b <branch-name> --single-branch <repository-url>

示例:

bash
git clone -b develop --single-branch https://github.com/user/repo.git

克隆指定分支(包含所有分支引用)

bash
git clone -b <branch-name> <repository-url>

示例:

bash
git clone -b feature-login https://github.com/user/repo.git

克隆后切换到指定分支

bash
git clone https://github.com/user/repo.git
cd repo
git checkout develop

查看所有远程分支

bash
git branch -r

输出:

origin/HEAD -> origin/main
origin/main
origin/develop
origin/feature-auth

创建本地分支跟踪远程分支

bash
git checkout -b develop origin/develop

浅层克隆

什么是浅层克隆

浅层克隆(Shallow Clone)只获取最近的提交历史,减少下载时间和磁盘空间。

基本用法

bash
git clone --depth <depth> <repository-url>

示例(只克隆最近 1 次提交):

bash
git clone --depth 1 https://github.com/user/repo.git

浅层克隆的特点

优点

  • 下载速度快
  • 占用空间小
  • 适合 CI/CD 环境

缺点

  • 缺少完整历史
  • 某些操作受限
  • 无法切换到所有分支

浅层克隆指定分支

bash
git clone --depth 1 -b main https://github.com/user/repo.git

转换为完整仓库

将浅层克隆转换为完整仓库:

bash
git fetch --unshallow

或指定深度:

bash
git fetch --depth=100

查看克隆深度

bash
git rev-parse --is-shallow-repository

输出 true 表示浅层克隆。

克隆子模块

什么是子模块

子模块(Submodule)允许在一个 Git 仓库中包含另一个 Git 仓库,常用于管理依赖项目。

克隆包含子模块的仓库

方法一:递归克隆

bash
git clone --recursive <repository-url>

方法二:克隆后初始化

bash
git clone https://github.com/user/repo.git
cd repo
git submodule init
git submodule update

更新子模块

更新所有子模块到最新提交:

bash
git submodule update --remote

克隆特定子模块

bash
git submodule update --init --recursive

查看子模块状态

bash
git submodule status

输出:

 abc123def456 path/to/submodule (heads/main)

子模块常用命令

初始化子模块:

bash
git submodule init

更新子模块:

bash
git submodule update

添加子模块:

bash
git submodule add <repository-url> <path>

删除子模块:

bash
git submodule deinit <path>
git rm <path>

克隆选项详解

指定克隆目录

bash
git clone <repository-url> <directory>

指定配置选项

bash
git clone --config user.name="John Doe" <repository-url>

使用模板创建

bash
git clone --template=<template-directory> <repository-url>

裸仓库克隆

bash
git clone --bare <repository-url>

裸仓库没有工作目录,常用于服务器端。

镜像克隆

bash
git clone --mirror <repository-url>

镜像克隆是裸仓库的变体,会复制所有引用。

指定分支为默认分支

bash
git clone -b <branch> <repository-url>

克隆不同协议

HTTPS 克隆

bash
git clone https://github.com/user/repo.git

特点:

  • 无需配置
  • 可能需要输入密码
  • 适合公开仓库

SSH 克隆

bash
git clone git@github.com:user/repo.git

特点:

  • 需要配置 SSH 密钥
  • 免密操作
  • 更安全

Git 协议克隆

bash
git clone git://github.com/user/repo.git

特点:

  • 速度最快
  • 只读访问
  • 不推荐使用

本地克隆

bash
git clone /path/to/local/repo.git

或使用 file 协议:

bash
git clone file:///path/to/local/repo.git

实战场景

场景一:参与开源项目

bash
git clone https://github.com/user/open-source-project.git
cd open-source-project
git checkout -b my-feature

场景二:CI/CD 环境快速克隆

bash
git clone --depth 1 --branch main https://github.com/user/repo.git

场景三:克隆大型仓库

bash
git clone --filter=blob:none --sparse https://github.com/user/large-repo.git
cd large-repo
git sparse-checkout init --cone
git sparse-checkout set src/components

场景四:备份远程仓库

bash
git clone --mirror https://github.com/user/repo.git repo-backup.git

最佳实践

选择合适的克隆方式

  • 完整克隆:日常开发,需要完整历史
  • 浅层克隆:CI/CD,快速部署
  • 单分支克隆:只需特定分支

使用 SSH 协议

bash
git clone git@github.com:user/repo.git

大型仓库使用稀疏检出

bash
git clone --filter=blob:none --sparse <repository-url>
git sparse-checkout set <paths>

克隆后检查状态

bash
git status
git branch -a
git remote -v

常见问题

克隆速度慢

解决方案:

  • 使用浅层克隆
  • 使用国内镜像
  • 检查网络连接

权限被拒绝

检查 SSH 密钥配置:

bash
ssh -T git@github.com

磁盘空间不足

使用浅层克隆:

bash
git clone --depth 1 <repository-url>

如何克隆私有仓库

确保有访问权限,使用认证方式:

bash
git clone https://username:token@github.com/user/private-repo.git

总结

  • git clone 是参与现有项目的第一步
  • 可根据需求选择完整克隆或浅层克隆
  • 使用 --recursive 克隆包含子模块的仓库
  • 推荐使用 SSH 协议提高安全性和便捷性
  • 大型仓库可使用稀疏检出节省空间