git 克隆
Downloading 仓库使用 git 克隆 — shallow clones, 深度限制, and 克隆 workflows.
你将学到
- 如何 克隆仓库 and 什么获取 downloaded
- difference 之间 HTTPS and SSH 克隆 URLs
- Cloning 到自定义 directory name
- Shallow clones 使用 --深度到 保存时间 and 空间
概念讲解
git clone 的作用
git 克隆 downloads a complete copy of a remote repository to your local machine — including the entire commit history, all branches, and all tags. It also automatically sets up a remote called 原点 pointing back to the source URL, so you can push and pull.
git clone https://github.com/user/repo.git
After cloning, you have a full-fledged local repository. You can commit, branch, view history, and push changes back — everything works offline except syncing with the remote.
HTTPS vs SSH URLs
You can clone using either protocol:
# HTTPS — uses username + personal access token
git clone https://github.com/user/repo.git
# SSH — uses your SSH key pair (no password prompts)
git clone [email protected]:user/repo.git
HTTPS is easier to set up but asks for credentials. SSH is more convenient after a one-time key setup and is the recommended approach for frequent contributors. Use the SSH Key Generator tool to create a key pair and add it to your GitHub account.
Cloning into a Custom Directory
By default, git 克隆 creates a directory named after the repository. You can specify a different name:
git clone https://github.com/user/repo.git my-custom-folder
Shallow Clones
Large repositories with years of history can take a long time to clone. A shallow clone downloads only the most recent commits instead of the full history:
# Clone only the last commit
git clone --depth 1 https://github.com/user/repo.git
# Clone the last 50 commits
git clone --depth 50 https://github.com/user/repo.git
This is useful for CI pipelines or when you only need the latest code. You can later deepen the history with git 获取 --unshallow.
What Gets Set Up Automatically
When you clone, Git automatically:
- Creates the local directory and copies all files
- Sets up the
.gitfolder with full history - Creates a remote named
原点pointing to the source URL - Checks out the default branch (usually
主要) - Sets up branch tracking (
局部 主要tracks原点/主要)
You can verify this with git remote -v and git 分支 -vv.
示例代码
# Clone a public repository (HTTPS)
git clone https://github.com/octocat/Hello-World.git
cd Hello-World
# Check what was set up automatically
git remote -v # shows origin pointing to the source
git log --oneline -5 # the full history is here
git branch -vv # shows tracking branch
# Clone using SSH (after setting up an SSH key)
git clone [email protected]:username/my-project.git
# Clone into a differently-named folder
git clone https://github.com/user/repo.git my-local-copy
# Shallow clone — only the latest commit (fast, small)
git clone --depth 1 https://github.com/user/huge-repo.git
# Clone a specific branch only
git clone --branch develop https://github.com/user/repo.git
# Later, deepen a shallow clone if you need full history
git fetch --unshallow
Covers 标准克隆, SSH 克隆, 自定义 directory naming, shallow clones for 大仓库, cloning 特定分支, and deepening shallow 克隆. 之后 cloning, git remote -v 确认原点 remote 是设置 automatically.
动手试一试
- SSH 键 Generator
Generate SSH 键 pair for cloning 通过 SSH 相反的 HTTPS.
- .gitignore Generator
之后 cloning, generate .gitignore for 任何新 files 你添加.
常见错误
错误写法
Cloning 到 directory 那个 already exists and 是不 空
正确做法
git 克隆创建新 directory. 如果目标 directory already exists and 是不 空, 克隆失败. 要么选择不同 name, or 克隆到 空 directory. 你能 也克隆到 current directory 使用 git 克隆 <URL> . 如果它 是空.
错误写法
Using HTTPS and 是 prompted for 密码每个时间
正确做法
GitHub 否 longer 接受 account 密码 for Git 操作. Generate Personal Access 令牌 (PAT) and use 它 as 你的密码, or 切换到 SSH 通过 generating 键 and 添加它 到你的 GitHub account.
相关资源
相关代码片段
- Git Remote Snippet
Managing remotes 之后 cloning.
相关速查表
- Git Cheatsheet
git 克隆 options and remote 设置 commands.
练习 git 克隆
6 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。