第2章 创建 GitHub 仓库 — 给项目盖一座房子
必读 想跟着这个阶段学下去,一定要读这一章。一句话总结:仓库(Repository)是存放项目文件和所有改动记录的地方。在 GitHub 上创建仓库,就在我的电脑之外有了一份安全的副本和协作空间。 公开还是私有,关系到以后能不能免费发布到网上,所以从创建时就要定下来。
这一章依据 2026年11月1日确认的 GitHub 官方文档编写。GitHub 的界面按英语来写。如果你在第3阶段用 Git 留存过“改动记录”,可以把这一步想成是把那份记录放到互联网上。
Git、GitHub 与仓库
Git 是追踪文件改动的版本管理系统。[1] 在第3阶段你对 Claude 说“帮我记录下来”,那个提交就是 Git 的记录。
GitHub 是把那个 Git 项目放到互联网上,并加上计划和协作工具的服务。[1:1] 而仓库是把项目的代码和文件放在 GitHub 上的地方。它保管文件,每次改动都用提交来记录,并成为一起工作的空间。[2]
常见的词,请先了解一下。
| 词 | 含义 |
|---|---|
| 分支(branch) | 不影响基础版本、单独工作的一条支线 |
| 合并(merge) | 把一条支线的改动并入另一条支线 |
| 远程(remote) | 位于 GitHub 上,而不是我电脑上的仓库 |
| 克隆(clone) | 把仓库连同记录一起下载到我的电脑 |
出处:GitHub 官方文档(仅摘录一部分)[3]
理解检查
关于 Git 和 GitHub 的关系,哪项是对的?
公开与私有
创建仓库时最先要定的是可见范围。
| 范围 | 谁能看到 |
|---|---|
| 公开(Public) | 互联网上的任何人 |
| 私有(Private) | 我和我分享给的人 |
出处:GitHub 官方文档[4]
这本指南选择 Public(公开)。因为这个选择与第7章的发布相关。免费账号只有在公开仓库里才能使用 GitHub Pages(免费的网页发布)。[5] GitHub 的入门教程也出于同样的理由,让人选择 Public。[6]
公开仓库是有代价的。全部代码对所有人可见,所以有人盯上弱点或敏感信息的风险会变大。[7] 因此,公开仓库里不要放含有个人信息·密码·真实姓名的备忘。 第9章会详细讲。
以后改成私有会怎样?(点击展开)
以后也可以更改,但会有代价。在免费账号下把公开仓库改成私有,已经发布的 Pages 网站会自动下线。[8]
理解检查
用免费账号,以后想发布到 GitHub Pages。仓库该怎么创建?
README·.gitignore·许可证
创建新仓库时,可以同时放进三个文件。[9]
- README:访客最先看到的说明书。[10] 写明这是一个做什么的项目、为什么有用、怎么开始。教程让人选择 Add README。因为它既是起始文件,又是介绍项目的地方。[11]
- .gitignore:写明提交时 Git 要忽略的文件的清单。[12] 像装有密码的设置文件这类不能上传的东西,就写在这里。要注意一点:已经提交过的文件,只是写进清单是不会被忽略的。[13] 所以最好一开始就写好。
- 许可证:告诉别人可以用我的代码做什么。不一定非选不可。[14] 如果不选,就适用默认的著作权法,别人不能复制·分发·修改后使用我的代码。[14:1] 如果是练习用的仓库,不选也可以。
如果想选许可证(点击展开)
如果不好选,GitHub 推荐 choosealicense.com 。GitHub 也声明,这份指引不是法律意见。[15]
上传已经有 Git 记录的文件夹时(点击展开)
像这本指南这样第一次新建仓库时,不用在意这一点。
导入已经有 Git 记录的仓库时,不要选这三项。GitHub 上新生成的文件和我文件夹里的记录可能会对不上。这时 Git 不知道该保留哪一边,就会停下来。这叫作合并冲突(merge conflict)。[16]
理解检查
关于 .gitignore,哪种说法是对的?
与我的电脑连接
把 GitHub 上的仓库带到我的电脑上,叫做克隆(clone)。克隆下来的副本和 GitHub 上的原件保持连接,所以我记录的内容可以再传上去。[17]
带过来的方法有好几种。
| 方法 | 适合的情况 |
|---|---|
| GitHub Desktop | 像官方入门教程那样,用图形界面 |
| 拜托 Claude Code | 想像第3阶段那样用说的来交代时 |
出处:本指南的整理(GitHub Desktop 是官方教程的方法)[17:1]
可以这样拜托 Claude Code。仓库地址,用在 GitHub 上打开仓库时浏览器地址栏里显示的地址(例:https://github.com/你的ID/cafe-todo-board)。
把这个 GitHub 仓库克隆到 ~/hello-ai-practice/todo-board 文件夹:<仓库地址>也可以把已有的文件夹传上去。 像第3阶段的 organizer 那样,原本就在我电脑里的文件夹也可以传到 GitHub 上,如果是一直没用 Git 管理的文件夹,要先把它做成 Git 仓库。[18] 不过本指南是把待办看板创建成新仓库。 organizer 里有备忘原件,原样放到公开仓库里,还是避免为好。
理解检查
把仓库克隆(clone)到我的电脑上,会怎样?
亲手试一试
- 在 github.com 注册或登录(免费账号就行)。
- 点右上角 + → New repository 创建新仓库。名称是
cafe-todo-board,可见范围选 Public,选择 Add README。如果能同时放进.gitignore,就放进去(清单按界面提示选)。 - 在仓库界面确认能看到 README。
- 用 GitHub Desktop 或 Claude Code,把仓库克隆到
~/hello-ai-practice/todo-board。以后把我电脑里的记录传到 GitHub 上,叫作推送(push)(第4章会讲)。要推送,那个工具必须已经登录我的 GitHub 账号。第一次上传时如果弹出登录窗口,照着提示做。 - 在 Claude Code 应用里打开克隆下来的文件夹,确认文件夹里有 README.md。
连续任务:在第3章里,我们接受 Claude 的采访,把这个仓库要做什么整理成 SPEC.md。
小结
- Git 是改动记录装置,GitHub 是把这份记录放在互联网上协作的地方,仓库是项目的家。
- 免费账号只有在公开仓库里才能使用 GitHub Pages。公开仓库里不要放个人信息。
- README 是说明书,.gitignore 是不上传的清单,许可证是使用许可。.gitignore 要从一开始就写好。
- 克隆下来的副本与 GitHub 保持连接。
术语整理
| 术语 | 中文名称 | 一句话解释 |
|---|---|---|
| Git | Git | 追踪文件改动的版本管理系统 |
| GitHub | GitHub | 把 Git 仓库放在互联网上协作的服务 |
| README | README | 仓库的第一份说明书 |
| .gitignore | .gitignore | Git 要忽略的文件清单 |
| Clone | 克隆 | 把仓库连同记录一起下载到我的电脑 |
这一章读完了吗?
学习记录只保存在这个浏览器中。整体进度可以在 全部目录中查看。
下一章:第3章. 策划与规格
参考资料(18 条)
GitHub 官方文档,《What is GitHub?》。"Git is a version control system that tracks changes to files." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Creating a repository for your project on GitHub》。"It stores your files, tracks each change as a commit, and gives collaborators a shared place to work." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《About repositories》。"Branch | A parallel version of your code that is contained within the repository, but does not affect the primary or main branch." / "Merge | To take the changes from one branch and apply them to another." / "Remote | A repository stored on GitHub, not on your computer." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《About repositories》。"Public repositories are accessible to everyone on the internet." / "Private repositories are only accessible to you, people you explicitly share access with, and, for organization repositories, certain organization members." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《What is GitHub Pages?》。"GitHub Pages is available in public repositories with GitHub Free and GitHub Free for organizations, and in public and private repositories with GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub Enterprise Server." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Creating a repository for your project on GitHub》。"Use the Choose visibility dropdown menu to select Public so you can publish your software project to a live site later in this series." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《About repositories》。"Public repositories expose your codebase to everyone, increasing the risk that attackers might exploit vulnerabilities or access sensitive information." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Setting repository visibility》。"Any published GitHub Pages site will be automatically unpublished." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Creating a new repository》。"If you're importing an existing repository to GitHub, don't choose any of these options, as you may introduce a merge conflict." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《About the repository README file》。"A README is often the first item a visitor will see when visiting your repository." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Creating a repository for your project on GitHub》。"Select Add README. This gives your repository a starting file and a place to describe your software project." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Ignoring files》。"You can create a
.gitignorefile in your repository's root directory to tell Git which files and directories to ignore when you make a commit." docs.github.com (确认日期 2026-11-01) ↩︎GitHub 官方文档,《Ignoring files》。"If you want to ignore a file that is already checked in, you must untrack the file before you add a rule to ignore it." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Licensing a repository》。"A software license tells others what they can and can't do with your source code, so it's important to make an informed decision." / "However, without a license, the default copyright laws apply, meaning that you retain all rights to your source code and no one may reproduce, distribute, or create derivative works from your work." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Licensing a repository》。"We hope it helps, but please keep in mind that we’re not lawyers and that we make mistakes like everyone else." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Creating a new repository》。"If you're importing an existing repository to GitHub, don't choose any of these options, as you may introduce a merge conflict." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Connecting to your code locally》。"Cloning creates a copy of your repository on your computer that stays connected to the version on GitHub." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,《Adding locally hosted code to GitHub》。"If your locally-hosted code isn't tracked by any VCS, the first step is to initialize a Git repository." docs.github.com (确认日期 2026-11-01) ↩︎