gh-pages 从 master 分支中删除我的源文件

gh-pages deletes my source files from the master branch

我按照 https://create-react-app.dev/docs/deployment/#github-pages 中的说明进行操作,注意到命令 gh-pages -b master -d build 将我工作目录的内容替换为 build 文件夹的内容,并将这些更改提交到 master分支。这就像在 myusername.github.io 上发布我的 React 应用程序所宣传的那样工作,但它使得无法对源进行进一步更改文件,因为它们在工作目录中不再可用。

我了解 GitHub 项目 页面与 GitHub 用户页面的工作方式不同(它们允许从 gh-pages 发布应用程序分支而不是 master)。那么,我是否必须将我的代码移动到新的项目存储库,或者有没有办法使用我现有的用户存储库来完成同样的事情?

无需移动代码。只需在 gh-pages 的第一个“更新”提交之前的提交中创建一个名为 source(或 dev,或任何你想称呼它的分支),并使用该分支,而不是 master,继续开发。

您可以通过更改 Github 上的存储库设置来维护 master 分支上的存储库代码,对于 Github 页面,使用 /root 目录和 select master 以外的新分支,然后,也更改您的部署脚本。

可选地,对于使用 create-react-app 构建的自定义域,您需要将 CNAME 文件复制到您的应用 public 目录。

Github 页面设置:

Github Pages settings using branch named production

添加可选的 CNAME 文件:

Add CNAME file to public directory for custom domain

更改脚本以指向新分支。在您的 package.json 文件中将 master 更新为新分支的名称。

变化:

"deploy": "gh-pages -b master -d build",

至:

"deploy": "gh-pages -b production -d build",

其中 production 是您的新分支的名称。