| | |

GitHub Pages 16 ๐Ÿ“„ Cheatsheet

โฑ๏ธ Estimated read: 3 min | |

This cheatsheet provides a high-density reference for deploying, securing, and troubleshooting GitHub Pages sites. It covers CLI commands, DNS configurations, and GitHub Actions snippets.


1. Git & GitHub CLI Essentials

Manual Subtree Deployment

Used to push a specific build folder (e.g., dist) to the gh-pages branch without including source code.

# Push 'dist' folder to 'gh-pages' branch
git subtree push --prefix dist origin gh-pages

# Force push a subtree (Fixes 'Reject' errors)
git push origin `git subtree split --prefix dist main`:gh-pages --force

Remote Branch Management

# Delete remote deployment branch
git push origin --delete gh-pages

# Create a fresh gh-pages branch from current state
git checkout --orphan gh-pages
git rm -rf .
git commit --allow-empty -m "Root commit for Pages"
git push origin gh-pages

GitHub CLI (gh)

# View deployment status
gh run list --workflow deploy.yml

# Clear Actions caches (fixes "Stale content" issues)
gh cache delete --all

# Enable Pages for a repository
gh repo edit --enable-pages

2. DNS & Custom Domains

GitHub Pages IP Addresses (A Records)

Point your apex domain (e.g., example.com) to these four IP addresses:

Record TypeIP Address
A185.199.108.153
A185.199.109.153
A185.199.110.153
A185.199.111.153

Subdomain Configuration (CNAME)

Point www or other subdomains to your GitHub profile URL:

  • Type: CNAME
  • Name: www
  • Target: username.github.io

3. GitHub Actions: Secure Deployment Snippet

Copy-paste this block into .github/workflows/deploy.yml for a secure, hardened deployment pipeline.

name: Deploy
on:
  push:
    branches: ["main"]
  workflow_dispatch:

# Hardened permissions: Minimal access required for Pages
permissions:
  contents: write
  pages: write
  id-token: write

# Prevent race conditions between multiple pushes
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      # Use immutable SHAs instead of tags for security
      - name: Checkout
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: Setup Node
        uses: actions/setup-node@60edb5dd545a775178f5252474591e516c138302 # v4.0.2
        with:
          node-version: 20
          cache: 'npm'

      - name: Build
        run: |
          npm ci
          npm run build -- --base=/${{ github.event.repository.name }}/

      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@65b5b28f402ac21313ca9b85144ca4419d701e4a # v4.5.0
        with:
          folder: dist
          branch: gh-pages

4. Static Site Generator (SSG) Configs

Vite (vite.config.js)

export default {
  // Required if your site is at username.github.io/repo-name/
  base: '/repo-name/', 
  build: {
    outDir: 'dist'
  }
}

Jekyll (_config.yml)

baseurl: "/repo-name" # The subpath
url: "https://username.github.io" # The domain

Next.js (next.config.js)

module.exports = {
  output: 'export', // Required for static export
  basePath: '/repo-name',
  images: { unoptimized: true } // GitHub Pages doesn't support Next.js Image Optimization
}

5. SEO & Performance Files

robots.txt

Place in the root of your source directory (e.g., public/ or static/).

User-agent: *
Allow: /
Sitemap: https://yourdomain.com/sitemap.xml

404.html SPA Hack

For React/Vue/Svelte apps using client-side routing, copy index.html to 404.html to prevent 404 errors on page refresh.

# Add to your build script
npm run build && cp dist/index.html dist/404.html

Canonical Link

Add to <head> to prevent duplicate indexing of .github.io and custom domains.

<link rel="canonical" href="https://www.yourdomain.com/current-page" />

6. Common Error Troubleshooting

IssueCauseFix
White Screen / 404 AssetsIncorrect Base PathSet base: '/repo/' in your SSG config.
Permission Denied (CI)GITHUB_TOKEN is Read-OnlyAdd permissions: contents: write to your YAML.
Custom Domain LostCNAME file deletedAdd CNAME to your public folder so it persists in dist.
SSL Not WorkingDNS propagation or CAA recordsEnsure all 4 A-Records are set; wait up to 24 hours.
Styles MissingRelative paths brokenUse {{ site.baseurl }} (Jekyll) or root-relative paths.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!