提交 ac38c097 authored 作者: zhangqianwei's avatar zhangqianwei

cfm_完成1.1.2 克隆项目

上级 913a48da
...@@ -293,7 +293,9 @@ A:GitHub 提供了 noreply 邮箱(`username@users.noreply.github.com`), ...@@ -293,7 +293,9 @@ A:GitHub 提供了 noreply 邮箱(`username@users.noreply.github.com`),
## 1.1.2 克隆项目 ## 1.1.2 克隆项目
### 什么是克隆(clone)? 一句话定义 ### 什么是克隆(clone)?
#### 一句话定义
`git clone <仓库地址>` 把远程仓库的**完整历史**复制到本地,生成一个全新的工作目录。 `git clone <仓库地址>` 把远程仓库的**完整历史**复制到本地,生成一个全新的工作目录。
...@@ -380,12 +382,10 @@ ssh -T git@github.com ...@@ -380,12 +382,10 @@ ssh -T git@github.com
### 克隆到指定目录 ### 克隆到指定目录
```bash ```bash
# 克隆并改名 # 克隆并改名(自动进入新目录)
git clone git@github.com:user/repo.git my-project git clone git@github.com:user/repo.git my-project
cd my-project
# 克隆到当前空目录(注意末尾的点) # 克隆到当前空目录(注意末尾的点,适用于已 mkdir 的空目录)
mkdir my-project && cd my-project
git clone git@github.com:user/repo.git . git clone git@github.com:user/repo.git .
``` ```
...@@ -396,7 +396,41 @@ git clone git@github.com:user/repo.git . ...@@ -396,7 +396,41 @@ git clone git@github.com:user/repo.git .
git clone --depth 1 git@github.com:user/repo.git git clone --depth 1 git@github.com:user/repo.git
# 后续如需看完整历史: # 后续如需看完整历史:
# git fetch --unshallow git fetch --unshallow
```
### 克隆指定分支
```bash
# 方法 1:克隆后切换到指定分支
git clone --depth 1 git@github.com:user/repo.git
git switch develop # 克隆默认是 main,改切到 develop
# 方法 2:直接克隆指定分支(适合分支很多的大型项目)
git clone --branch develop git@github.com:user/repo.git
```
### 克隆后的标准流程
```bash
# 1. 进入仓库目录
cd my-project
# 2. 查看克隆后的状态(自动配置好了 origin 远程)
git remote -v
# origin git@github.com:user/repo.git (fetch)
# origin git@github.com:user/repo.git (push)
# 3. 查看当前分支
git branch -a
# * main
# remotes/origin/HEAD -> origin/main ← 默认分支
# remotes/origin/develop
# remotes/origin/feature/xxx
# 4. 克隆后第一件事:配置你的身份(见 1.1.1)
git config user.name "张三"
git config user.email "zhangsan@company.com"
``` ```
--- ---
...@@ -407,20 +441,38 @@ git clone --depth 1 git@github.com:user/repo.git ...@@ -407,20 +441,38 @@ git clone --depth 1 git@github.com:user/repo.git
A:SSH Key 没配好。检查: A:SSH Key 没配好。检查:
1. 私钥路径对不对:`ls ~/.ssh/id_ed25519` 1. 私钥路径对不对:`ls ~/.ssh/id_ed25519`(Linux/Mac)或 `ls ~/.ssh/id_rsa`(Windows)
2. 公钥是否已加到 GitHub:`cat ~/.ssh/id_ed25519.pub` 2. 公钥是否已加到 GitHub:`cat ~/.ssh/id_ed25519.pub`
3. SSH agent 加载了没有:`ssh-add ~/.ssh/id_ed25519` 3. SSH agent 加载了没有(Linux/Mac):
```bash
ssh-add ~/.ssh/id_ed25519
```
4. Windows 用户:用 "Git Bash" 或设置 Pageant 等 SSH agent
**Q:克隆很慢/超时?** **Q:克隆很慢/超时?**
A:换协议试试: A:几种解决方案:
```bash 1. 换协议试试(在国内 SSH 通常比 HTTPS 快):
# 用 SSH 替代 HTTPS(在国内通常更快)
git clone git@github.com:user/repo.git ```bash
``` git clone git@github.com:user/repo.git
```
2. 配代理(如果有梯子):
```bash
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
```
3. 用国内镜像(如 Gitee)先 fork 再克隆
**Q:GitHub 限制单个仓库 100MB,克隆大仓库失败?**
A:GitHub 对大文件有限制。如果仓库包含大文件:
或者配 git proxy:`git config --global http.proxy http://127.0.0.1:7890` 1. 先用浅克隆:`git clone --depth 1 <url>`
2. 如果是 Git LFS 仓库:`git lfs install` 后再克隆
3. 联系仓库管理员清理大文件或用 Git LFS 管理
**Q:克隆完发现忘了 --depth,怎么补全历史?** **Q:克隆完发现忘了 --depth,怎么补全历史?**
...@@ -438,8 +490,10 @@ git fetch --unshallow ...@@ -438,8 +490,10 @@ git fetch --unshallow
☐ 能用 git clone <ssh-url> 把项目拉下来 ☐ 能用 git clone <ssh-url> 把项目拉下来
☐ 能区分 HTTPS 和 SSH 两种协议的使用场景 ☐ 能区分 HTTPS 和 SSH 两种协议的使用场景
☐ 能生成 SSH Key 并配置到 GitHub ☐ 能生成 SSH Key 并配置到 GitHub
☐ 知道克隆后默认进入 master/main 分支 ☐ 知道克隆后远程 origin 自动配置好,查看用 git remote -v
☐ 能用 --depth 1 做浅克隆,并能用 --unshallow 补全历史 ☐ 能用 --depth 1 做浅克隆,并能用 --unshallow 补全历史
☐ 能用 --branch 克隆指定分支
☐ 知道克隆后要配置 git 身份信息
``` ```
## 官方来源 ## 官方来源
...@@ -449,7 +503,8 @@ git fetch --unshallow ...@@ -449,7 +503,8 @@ git fetch --unshallow
## 参考文献 ## 参考文献
[1] *Pro Git* 第 2.1 节 "获取帮助"。 [1] Scott Chacon & Ben Straub. *Pro Git (2nd Edition)*. Apress, 2014. 第 2.1 节 "Git 基础 - 获取 Git 仓库".
[2] GitHub Docs. *Cloning a repository*. [https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository)
--- ---
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论