跳至主要内容

从模板创建代码空间

如果你正准备开启一个新项目,可以从空白模板创建代码空间,或选择专为你想要进行的工作类型设计的模板。

关于 GitHub Codespaces 的模板

如果你正准备开启一个新项目,使用模板创建代码空间可以让你快速开始开发工作。你可以在基于云的开发环境中进行项目开发,将文件保存在云端,并将工作发布到一个新的远程仓库,便于与他人共享或克隆到本地机器。

你可以从空白模板开始,也可以从 GitHub 为流行技术(如 React 或 Jupyter Notebook)维护的模板中选择,或直接从 GitHub 上的任意模板仓库启动代码空间。

使用空白模板时,你将拥有一个空目录,并可使用云计算资源以及默认开发容器镜像中预装的工具、语言和运行时环境。使用其他模板时,你将获得对应技术的入门文件,通常还包括 README、.gitignore 文件以及包含自定义环境配置的开发容器配置文件。有关开发容器及默认镜像的更多信息,请参阅dev 容器简介

举例来说,如果你使用 GitHub 的 React 模板创建代码空间,工作区将包含一个简单应用的模板文件,如 index.jsapp.jspackage.json。代码空间打开后不久,开发服务器会自动启动,你可以在 VS Code Web 客户端的简易浏览器标签页中查看正在运行的应用。

Screenshot of VS Code's simple browser rendering the web application in GitHub's React template.

模板中包含的文件和配置定义于模板仓库。当你创建代码空间时,会将该模板仓库克隆到你的代码空间中。之后,两者的关联即被切断,除非你手动将代码空间发布到远程仓库,否则它不会再与任何仓库关联。

提示

为了帮助人们快速上手你的框架、库或其他项目,你可以为 GitHub Codespaces 设置一个模板仓库。更多信息请参阅为 GitHub Codespaces 设置模板仓库

从 GitHub 模板创建代码空间

GitHub 维护的模板(包括空白模板)可在“Your codespaces”页面获取。

  1. 在 GitHub 左上角,选择,然后点击 Codespaces 以打开位于 github.com/codespaces 的 “Your codespaces” 页面。

  2. 要查看完整模板列表,请在“Explore quick start templates”章节中点击See all(查看全部)。

    Screenshot of the "Explore quick start templates" section. "See all" is highlighted with a dark orange outline.

  3. 如需查看包含模板文件的模板仓库,可点击模板名称。

    Screenshot of the "Explore quick start templates" section. Three templates are listed. The templates names are outlined in orange.

  4. 在想要启动的模板名称下,点击Use this template(使用此模板)。

从模板创建的新代码空间始终在 Visual Studio Code Web 客户端中打开。你可以在任何受支持的编辑器中重新打开已有的代码空间。更多信息请参阅打开已有代码空间

从模板仓库创建代码空间

你可以从任意模板仓库创建代码空间,完成后再将工作发布到新仓库。有关模板仓库的更多信息,请参阅从模板创建仓库

  1. 在 GitHub 上,导航至仓库的主页面。

  2. 点击 Use this template(使用此模板),然后点击 Open in a codespace(在代码空间中打开)。

    Screenshot of the "Use this template" button and the dropdown menu expanded to show the "Open in a codespace" option.

    注意

    如果你是模板仓库的维护者,并且想要对模板仓库本身提交更改,应从 代码下拉框创建代码空间。更多信息请参阅为仓库创建代码空间

从模板创建的新代码空间始终在 Visual Studio Code Web 客户端中打开。你可以在任何受支持的编辑器中重新打开已有的代码空间。更多信息请参阅打开已有代码空间

将代码发布到 GitHub 上的仓库

在基于模板创建的代码空间中工作时,你的工作会保存在云端的虚拟机上,但不会自动存入 GitHub 上的仓库。

你可以保存文件、关闭并停止代码空间,之后再继续工作。通常情况下,Git 已预装,工作目录会自动初始化为 Git 仓库(除非你使用的是 GitHub 的空白模板),因此你可以立即使用 Git 进行本地源码管理,例如添加和提交文件。

然而,如果你删除了未发布的代码空间,或因长时间未使用而在保留期限结束后被自动删除,则你的工作也会一起被删除。若要持久化工作并让他人能够协作,你需要将代码空间发布到 GitHub 上的仓库。

注意

如果未发布的代码空间当前计费方为组织,发布代码空间后会把所有权和计费转移到你的个人账户。详情请参阅GitHub Codespaces 计费

从 VS Code 发布

在代码空间内工作时,你可以通过 VS Code Web 客户端或桌面应用进行发布。

  1. 在活动栏中,单击 源代码管理 视图。

    Screenshot of the VS Code Activity Bar with the source control button highlighted with an orange outline.

  2. 要暂存更改,请点击+图标,位于已添加或修改的文件旁,或在Changes(更改)旁点击以一次性暂存所有已修改的文件。

    Screenshot of the "Source control" side bar with the staging button (a plus sign), to the right of "Changes," highlighted with a dark orange outline.

    注意

    如果你使用 GitHub 的空白模板,除非已将目录初始化为 Git 仓库,否则不会看到更改列表。要发布从空白模板创建的代码空间,请在“Source Control”(源代码管理)视图中点击Publish to GitHub(发布到 GitHub),随后直接跳到第 5 步。

  3. 要提交暂存的更改,请输入描述本次修改的提交信息,然后单击 Commit

    Screenshot of the "Source control" side bar with a commit message and, below it, the "Commit" button both highlighted with a dark orange outline.

  4. 单击 Publish Branch

    Screenshot of the "Source control" side bar showing the "Publish Branch" button.

  5. 在 “Repository Name” 下拉框中输入新仓库的名称,然后选择 Publish to GitHub private repository(发布到 GitHub 私有仓库)或 Publish to GitHub public repository(发布到 GitHub 公开仓库)。

    Screenshot of the repository name dropdown in VS Code. Two options are shown, for publishing to a private or a public repository.

    新仓库的所有者将是创建代码空间时所使用的 GitHub 账户。

  6. 可选:在编辑器右下角弹出的提示框中,点击Open on GitHub(在 GitHub 上打开),即可查看新创建的仓库。

    Screenshot of a confirmation message for a successfully published repository, showing the "Open on GitHub" button.

代码空间发布后,你可以使用更多自定义选项来优化 GitHub Codespaces 体验。例如,你可以

从 GitHub 发布

你可以在 GitHub 的 “Your codespaces” 页面上发布未发布的代码空间。这在你想发布当前未在浏览器中打开的代码空间时非常有用。发布后,你的工作会保存在仓库中,但现有代码空间与新仓库之间不会形成关联。不过,你可以进入新仓库并从那里创建代码空间,该代码空间将与仓库相连。

  1. 在 GitHub 左上角,选择,然后点击 Codespaces 以打开位于 github.com/codespaces 的 “Your codespaces” 页面。

  2. 在未发布的代码空间旁,点击省略号(...),然后选择Publish to a new repository(发布到新仓库)。

    Screenshot of the dropdown menu for a codespace, showing the "Publish to a new repository" option.

  3. 为新仓库指定名称,设置为Public(公开)或Private(私有),然后点击Create repository(创建仓库)。

    Screenshot of the "Publish to a new repository" dropdown, with the "Name" field, "Public" and "Private" options, and "Create repository" button.

  4. 可选:若要查看新仓库,点击See repository(查看仓库)。

延伸阅读

© . This site is unofficial and not affiliated with GitHub, Inc.