GitHub Tutorial
- 1. Introduction to GitHub
- 2. Account Setup & SSH
- 3. Repository Management
- 4. Remotes & Syncing
- 5. Collaborating Via Branches
- 6. Forking & Pull Requests
- 7. Code Reviews & Branch Protection
- 8. Merge Strategies
- 9. Issues & Projects
- 10. Markdown & Project Wikis
- 11. Deploying to GitHub Pages
- 12. CI/CD with GitHub Actions
- 13. Packages & Releases
- 14. Gists & Discussions
- 15. Security & Dependabot
11. Deploying to GitHub Pages
One of GitHub's most amazing features is **GitHub Pages**. It allows you to host static web sites (HTML, CSS, JavaScript, images) directly from a GitHub repository for **absolutely free**. It is the perfect way to publish portfolios, landing pages, documentation sites, or small client demos.
1. Preparing Your Site for Deployment
GitHub Pages only hosts static files. This means it **cannot** host server-side applications (like PHP, Node.js, Express, or Python backends) or dynamic database connections.
To deploy:
- Ensure your repository root contains an entry file named exactly
index.html. - Ensure all file path links (CSS, JS, images) are **relative** paths (e.g., use
./styles.cssinstead of absolute paths).
2. Deploying a Branch (Step-by-Step)
The easiest way to deploy is directly from your default branch (e.g., main):
- Commit and push your static web files to your GitHub repository.
- Navigate to your repository on GitHub and click the Settings tab.
- In the left-hand sidebar menu under "Code and automation", click "Pages".
- Under "Build and deployment", set the Source to "Deploy from a branch".
- Under "Branch", select your branch (e.g.,
main) and the directory (usually/ (root)). - Click the green "Save" button.
Within a minute, a banner will appear at the top of the Pages settings page showing your live URL, which follows this structure:
https://username.github.io/repo-name/3. Using a Custom Domain
If you want to use your own professional domain (e.g., www.myportfoliosite.com) instead of the default github.io address:
- Go back to your repository **Settings > Pages**.
- Under "Custom domain", type your domain name and click **Save**.
- Go to your Domain Registrar dashboard (e.g., GoDaddy, Namecheap) and configure your DNS settings:
- Add an CNAME Record pointing to
username.github.io. - Or add A Records pointing to GitHub Pages IP addresses:
185.199.108.153 185.199.109.153 185.199.110.153 185.199.111.153
- Add an CNAME Record pointing to
- Back on GitHub, check the box to "Enforce HTTPS" to ensure your custom site has an SSL security certificate!
4. Deploying via GH-Pages Dependency
If you are working with React or Vue and want to automate deployments via NPM packages:
# 1. Install the deployment tool
npm install gh-pages --save-dev
# 2. Add scripts to your package.json
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
# 3. Deploy to GitHub Pages with one command
npm run deploy