Git: Git克隆仓库与远程协作入门

克隆(Clone)是从远程服务器获取完整Git仓库副本的操作,包含所有文件、分支和完整的提交历史。这是参与团队协作的第一步。

1. 克隆概述

(1) 什么是克隆?

克隆是将远程仓库完整复制到本地的过程。与下载ZIP压缩包不同,克隆会获得完整的Git仓库,包括:

(2) 克隆与下载的区别

很多人会问:为什么不直接下载ZIP?让我们对比一下:

操作 包含内容 能否查看历史 能否推送修改 能否切换版本
git clone 完整仓库
下载ZIP 仅文件快照

(3) 克隆的应用场景

克隆主要用于以下场景:


2. git clone命令

(1) 基本语法

git clone命令的基本语法:

BASH
# 克隆到当前目录(自动创建同名文件夹)
git clone <仓库地址>

# 克隆到指定目录
git clone <仓库地址> <目录名>

(2) 克隆流程

克隆的完整流程如下:

100%
sequenceDiagram
    participant L as 本地仓库
    participant R as 远程服务器
    
    L->>R: 1. 发起克隆请求
    R->>L: 2. 发送仓库元数据
    R->>L: 3. 发送所有对象(文件、提交)
    R->>L: 4. 发送分支信息
    L->>L: 5. 创建origin远程关联
    L->>L: 6. 检出默认分支
    L->>L: 7. 克隆完成

▶ 示例:克隆GitHub仓库

BASH
# 克隆React项目(HTTPS方式)
git clone https://github.com/facebook/react.git

# 输出信息:
# Cloning into 'react'...
# remote: Enumerating objects: 156789, done.
# remote: Counting objects: 100% (156789/156789), done.
# remote: Compressing objects: 100% (456/456), done.
# remote: Total 156789 (delta 12345), reused 156333 (delta 12000), pack-reused 0
# Receiving objects: 100% (156789/156789), 45.67 MiB | 5.23 MiB/s, done.
# Resolving deltas: 100% (123456/123456), done.

# 克隆到指定目录
git clone https://github.com/facebook/react.git my-react-project

# 克隆到当前目录(注意最后的点)
git clone https://github.com/facebook/react.git .

▶ 示例:克隆后查看信息

BASH
# 克隆仓库
git clone https://github.com/user/demo.git
cd demo

# 查看远程仓库关联
git remote -v
# origin  https://github.com/user/demo.git (fetch)
# origin  https://github.com/user/demo.git (push)

# 查看所有分支(包括远程分支)
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature

# 查看提交历史
git log --oneline -5
# a1b2c3d (HEAD -> main, origin/main, origin/HEAD) Update README
# e4f5g6h Add new feature
# i7j8k9l Fix bug in login
# m0n1o2p Initial commit

3. 克隆选项

(1) 浅克隆

对于大型项目,完整克隆可能需要很长时间和大量空间。浅克隆只下载最近的几次提交:

BASH
# 只克隆最近一次提交
git clone --depth 1 <仓库地址>

# 克隆最近N次提交
git clone --depth 10 <仓库地址>

浅克隆的特点:

(2) 单分支克隆

如果只需要某个分支,可以只克隆该分支:

BASH
# 只克隆指定分支
git clone --branch main --single-branch <仓库地址>

# 简写形式
git clone -b main --single-branch <仓库地址>

(3) 递归克隆

项目如果包含子模块(Submodule),需要递归克隆:

BASH
# 递归克隆,同时克隆所有子模块
git clone --recursive <仓库地址>

# 如果已经克隆,可以单独初始化子模块
git clone <仓库地址>
cd <目录>
git submodule update --init --recursive

(4) 克隆选项对比

选项 说明 下载量 适用场景
无选项 完整克隆 全部 日常开发
--depth 1 浅克隆 仅最新提交 CI/CD、部署
--single-branch 单分支 仅指定分支 只需某个分支
--recursive 递归克隆 含子模块 项目有子模块
--bare 裸仓库 无工作区 服务器镜像

▶ 示例:浅克隆大型项目

BASH
# Linux内核完整克隆(3GB+,耗时很长)
git clone https://github.com/torvalds/linux.git
# Cloning into 'linux'...
# Receiving objects: 100% (8234567/8234567), 3.45 GiB | 1.23 MiB/s, done.

# 浅克隆Linux内核(100MB+,速度快)
git clone --depth 1 https://github.com/torvalds/linux.git
# Cloning into 'linux'...
# Receiving objects: 100% (12345/12345), 156.78 MiB | 5.67 MiB/s, done.

# 查看历史差异
cd linux
git log --oneline
# 只有1次提交(浅克隆)

# 完整克隆的仓库
git log --oneline | wc -l
# 100万+次提交

4. 远程地址

(1) 协议类型

Git支持多种协议访问远程仓库:

100%
graph TB
    A[远程仓库地址] --> B[HTTPS协议]
    A --> C[SSH协议]
    A --> D[Git协议]
    A --> E[本地路径]
    
    B --> B1[Example: https://...]
    C --> C1[Example: git@...]
    D --> D1[Example: git://...]
    E --> E1[Example: /path/to/...]
    
    style B fill:#d4edda
    style C fill:#c3e6cb
    style D fill:#fff3cd
    style E fill:#f8d7da

(2) HTTPS vs SSH

最常用的是HTTPS和SSH两种协议:

特性 HTTPS SSH
地址格式 https://github.com/user/repo.git git@github.com:user/repo.git
配置难度 简单,无需配置 需要生成和配置SSH密钥
认证方式 用户名+密码/Token SSH密钥
推送操作 每次需要输入凭证 无需输入密码
防火墙 友好,使用443端口 可能被阻止,使用22端口
推荐场景 新手、临时使用 日常开发、自动化

(3) 使用Personal Access Token

GitHub从2021年起不再支持密码认证,需要使用Personal Access Token:

BASH
# 克隆时使用Token
git clone https://<token>@github.com/user/repo.git

# 或在推送时输入Token作为密码
git clone https://github.com/user/repo.git
# Username: your-username
# Password: ghp_xxxxxxxxxxxx(使用Token)

▶ 示例:使用不同协议克隆

BASH
# HTTPS方式(推荐新手)
git clone https://github.com/facebook/react.git

# SSH方式(推荐熟练开发者)
git clone git@github.com:facebook/react.git

# Git协议(只读,较少使用)
git clone git://github.com/facebook/react.git

# 本地路径(克隆本地仓库)
git clone /path/to/local/repo.git

# 从同一台服务器的其他用户目录克隆
git clone file:///home/otheruser/project.git

5. 克隆后配置

(1) 自动创建的关联

克隆完成后,Git会自动创建以下配置:

100%
graph TB
    A[克隆的仓库] --> B[.git目录]
    A --> C[工作区文件]
    A --> D[远程关联origin]
    
    B --> B1[所有提交历史]
    B --> B2[所有分支信息]
    B --> B3[配置文件]
    
    D --> D1[fetch地址]
    D --> D2[push地址]
    D --> D3[默认分支追踪]
    
    style A fill:#e1f5ff
    style D fill:#d4edda

(2) 查看远程信息

BASH
# 查看远程仓库名称
git remote
# origin

# 查看详细信息
git remote -v
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

# 查看某个远程仓库的详细信息
git remote show origin
# * remote origin
#   Fetch URL: https://github.com/user/repo.git
#   Push  URL: https://github.com/user/repo.git
#   HEAD branch: main
#   Remote branches:
#     main     tracked
#     develop  tracked
#   Local branch configured for 'git pull':
#     main merges with remote main
#   Local ref configured for 'git push':
#     main pushes to main (up to date)

(3) 克隆后的分支

克隆会获取所有远程分支,但只会检出默认分支:

BASH
# 查看本地分支
git branch
# * main  (只有默认分支)

# 查看所有分支(包括远程)
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature-login

# 创建本地分支追踪远程分支
git checkout -b develop origin/develop
# 或使用简写
git checkout develop
# Branch 'develop' set up to track remote branch 'develop' from 'origin'.
# Switched to a new branch 'develop'

▶ 示例:克隆后的完整操作流程

BASH
# 1. 克隆仓库
git clone https://github.com/user/project.git
cd project

# 2. 查看状态
git status
# On branch main
# Your branch is up to date with 'origin/main'.

# 3. 查看远程信息
git remote -v

# 4. 查看所有分支
git branch -a

# 5. 切换到其他分支
git checkout develop

# 6. 查看提交历史
git log --oneline --graph --all -10

# 7. 拉取最新更新
git pull origin main

❓ 常见问题

Q 克隆和下载ZIP有什么本质区别?
A 克隆获得的是完整的Git仓库,包含所有历史记录、分支信息和版本控制元数据。你可以查看历史、切换版本、创建分支、推送修改。下载ZIP只是文件快照,没有版本控制功能,无法进行任何Git操作。
Q 克隆大型仓库很慢怎么办?
A 可以使用浅克隆--depth 1只下载最新提交,速度会快很多。如果只需要某个分支,使用--single-branch选项。另外,可以使用国内镜像站点加速,如GitHub的fastgit镜像。
Q 克隆后如何推送到远程仓库?
A 需要有推送权限。公开仓库只能克隆不能推送。对于有权限的仓库,使用HTTPS需要输入用户名和Token,使用SSH需要配置好SSH密钥。推送命令是git push origin <分支名>
Q 如何克隆私有仓库?
A 私有仓库需要认证。HTTPS方式使用用户名和Personal Access Token,SSH方式使用配置好的SSH密钥。确保你的账号有访问权限,否则会提示Authentication failed。
Q 克隆时提示"fatal: destination path already exists"怎么办?
A 目标目录已存在。可以删除已存在的目录后重新克隆,或克隆到其他目录git clone <地址> <新目录名>,或进入已存在的目录执行git pull更新。

📖 小节


📝 作业

  1. 基础题:克隆GitHub上的Hello-World仓库,查看远程信息和所有分支,理解克隆后的目录结构。

  2. 进阶题:对比完整克隆和浅克隆--depth 1一个大型项目(如React),比较下载时间和.git目录大小,理解浅克隆的应用场景。

  3. 挑战题:研究如何将一个仓库克隆到本地后,推送到另一个远程仓库(如从GitHub克隆后推送到GitLab),理解远程仓库的管理和多远程配置。

Web-Tutorial.com

Web-Tutorial 技术团队

由多位开发者共同维护的编程教程平台。每篇教程由对应领域的开发者编写和审核,确保内容准确可靠。如发现任何问题,欢迎向我们反馈。

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏