克隆Clone
Git 专区第一次把网上的整个代码仓库下载到电脑
clone 会下载代码、分支信息和提交历史,并创建新的项目文件夹。进入目录后先读 README 和项目配置,再安装依赖;以后同步增量更新使用拉取 Pull。
远程仓库my-first-page
git clone→
本地新文件夹
$ git clone https://github.com/oil-oil/my-first-page.git
clone 是第一次整仓下载:代码连全部存档历史一起下来,git log 随便翻。之后拿更新用 git pull 只取增量,别用 Download ZIP 代替——没有 .git 历史。
什么时候用
- 绿色 Code 按钮复制 HTTPS 地址:贴进终端,一回车就下载<> Code ▾https://github.com/oil-oil/my-first-page.git📋绿色 Code 按钮,复制 HTTPS 地址
- clone 一次就有完整历史:所有存档点都在,git log 随便翻$ git log --onelinee7b2d48 修复按钮在手机上错位a3f9c21 完成首页导航栏0f1e2d3 初始化项目
- 之后拿更新用 拉取 Pull 增量拉:不用每次重新 clone第一次 clone 整仓→之后 pull 增量
- 分清 fork 和 clone:fork 在云端复制一份,clone 下载到本地⑂ Fork→你账号下多一份仓库fork 在云端复制,clone 下载到本地
什么时候不用
- 把下载 ZIP 当成 clone;压缩包不包含 Git 历史Download ZIP→git log 一片空白ZIP 只有代码没有 .git,存档历史全丢
- 想改别人的项目只 clone 不 fork:push 时才发现根本没权限$ git pushremote: Permission to oil-oil/my-first-page.git denied.
- 反复 clone 出 xxx-2、xxx-最新:三份代码分不清哪个新📁 my-first-page
📁 my-first-page-2
📁 my-first-page-最新clone 三份,根本分不清哪个新 - 取得项目后没有安装依赖,就直接尝试启动$ npm run devError: Cannot find module 'react'
组成结构 · Anatomy
https://github.com/oil-oil/my-first-page.git
1协议HTTPS新手可以先复制 HTTPS 地址,不需要另外配置 SSH 密钥
2平台Host仓库托管在哪个网站,GitHub、Gitee 都长这样
3账号名Owner仓库是谁的,fork 之后这一段会变成你的名字
4仓库名Repository项目名字,clone 下来的文件夹默认就叫它
常见变体 · Variants
整仓下载git clone
git clone https://github.com/oil-oil/my-first-page.git
连代码带全部历史一次到位
改文件夹名clone + 名字
git clone <地址> my-app
想换个本地文件夹名
只要代码Download ZIP
Code ▾ → Download ZIP
不要历史可以,但别当 clone
改别人项目Fork
网页右上 ⑂ Fork
先叉到自己账号,再 clone
典型使用场景
GitHub Code 按钮复制地址
终端 git clone 下载
$ git clone https://github.com/oil-oil/my-first-page.git
Cloning into 'my-first-page'...
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
Receiving objects: 100% (36/36), 12.4 KiB | 2.1 MiB/s, done.
当前目录多了一个 my-first-page 文件夹
clone 完翻全部历史
$ git log --oneline
e7b2d48 修复按钮在手机上错位
a3f9c21 完成首页导航栏
b4c5d67 首页能跑了
0f1e2d3 初始化项目
刚 clone 下来就有全部历史,一个不少
fork 别人的仓库