Tecnate | Last Updated: 2024-06-21 |
This is a quick guide to get React apps created with the Create React App up and running on GitHub pages. If you need a Git refresher, refer to my Git Cheatsheet.
At this time, you can only run static applications using GitHub pages; it’s not meant to get complex, dynamic react apps online.
I found Deploying a React App to GitHub Pages to be a great help. Please refer to it for more detailed explanations.
This is an overview. Refer to the Deployment Process section for step-by-step instructions.
The development-deployment workflow for running a React app on GitHub pages generally looks like this:
npm run deploy
from your main branch to publish your app to GitHub Pages.Decide which you’d rather do for further development:
/build
to your .gitignore file (explained in more detail below).Install gh-pages
as a dependency:
npm install gh-pages --save-dev
Add a homepage
property to the top of your package.json file in the following format:
https://{username}.github.io/{repo-name}
https://{username}.github.io
// package.json file example
{
"name": "my-app",
"version": "0.1.0",
"homepage": "https://nvsmith.github.io/landing-page",
"private": true,
...
}
Add these predeploy
and deploy
properties to the scripts object to your package.json file:
// Only add predeploy & deploy properties, leave everything else alone
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
...
}
Create a new branch exactly named gh-pages and push it upstream to GitHub before deployment:
# Create new branch for GitHub pages
git checkout -b gh-pages
# Set upstream for deployment
git push -u origin gh-pages
npm deploy
will automatically create the gh-pages branch, but it’s never worked for me. So you can just do it manually before running deployment if you experience the same issues.Checkout your main branch again and deploy your app to GitHub pages:
# Checkout main
git checkout main
# Deploy app to static website with a comment
npm run deploy -- -m "Deploy React app to GitHub Pages"
Here’s what happens:
predeploy
script you added creates a distributable React app in a folder name build, which contains a folder named static.deploy
script you added pushes the static folder to your remote gh-pages branch along with the commit SHA and comment; this does not push to your main branch.
npm run deploy
: This command is not a standard built-in script in React projects but is often used to automate the deployment process to hosting services (like GitHub Pages). It includes the standardnpm run build
command (to prepare app for production) and then deploy the contents of the build directory to the hosting service.
gh-pages
/ (root)
Whether you decide to commit the changes made during deployment will depend on whether or you want the source code to be made available or not.
You do not have to commit/sync changes to your gh-pages branches once your start deploying. In fact, you can delete the local gh-pages branch if you want to, since the remote branch will always be built directly from a deployment on main.
Back in terminal, commit the deployment build changes to main and push to remote:
# Check you're on main & only have build changes
git status
# Add files and commit changes
git add . && git commit -m "Add distributable source code to repo"
# Push to GitHub
git push
At this point, your main branch will be ahead of your development branch. For further development work, decide if you want to:
# Checkout your development branch (or create a new one)
git checkout development
# Fetch & merge changes from remote main into local development
git pull origin main
And repeat the develpment-deployment cycle:
You can manually discard changes, as demonstrated below, or you can simply add /build
to your .gitignore file.
Back in terminal, ignore any changes made during deployment.
# Check you're on main & only have build changes
git status
# Discard all changes and revert to prior commit state
git checkout .
# Remove untracked files and directories
git clean -df
# Move back to a development or feature branch to continue working
git checkout development
And repeat the cycle of development:
npm run deploy
.git checkout .
git clean -df