Search code examples
create-react-appgithub-pages

gh-pages deletes my source files from the master branch


I followed the instructions at https://create-react-app.dev/docs/deployment/#github-pages and noticed that the command gh-pages -b master -d build replaced the contents of my working directory with the contents of the build folder and committed these changes to the master branch. This works as advertised to publish my React app at myusername.github.io, but it makes it impossible to make further changes to the source files since they are no longer available in the working directory.

I understand that GitHub project pages work differently than GitHub user pages (they allow the application to be published from the gh-pages branch rather than master). So, do I have to move my code to a new project repository or is there a way to accomplish the same thing using my existing user repository?


Solution

  • You can maintain your repositories code on the master branch by changing the repository settings on Github, for Github Pages, use the /root directory and select a new branch other than master, then, also change your deploy script.

    Optionally, for a custom domain built using create-react-app, you will need to and a CNAME file to the your apps public directory.

    Github Pages Settings:

    Github Pages settings using branch named production

    Add optional CNAME file:

    Add CNAME file to public directory for custom domain

    Change the script to point to the new branch. In your package.json file update master to the name of your new branch.

    Change:

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

    to:

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

    Where production is the name of your new branch.