GitHub Pages
GitHub Pages hosts a static website (HTML, CSS, JavaScript, images) straight from a repository, for free, at https://<user>.github.io/<repo>/. It's ideal for project documentation, portfolios and small sites, and it is just Git: you publish by pushing.
How Pages picks what to publish
In the repository's Settings → Pages, you choose a source:
| Source | Publishes |
|---|---|
Deploy from a branch: main, folder / (root) |
the files at the root of main |
Deploy from a branch: main, folder /docs |
only the docs/ folder of main |
Deploy from a branch: gh-pages |
a separate branch holding only the built site |
| GitHub Actions | a workflow builds the site (e.g. npm run build) and uploads the result |
A repository named <user>.github.io becomes your personal site at https://<user>.github.io/. Pages expects an index.html (or index.md, rendered with Jekyll) at the top of the published folder.
Publishing from a docs/ folder
The simplest setup keeps the site next to the code. Push the folder, then pick main /docs in the settings:
git init -q --bare ~/github/ada.github.io.git
mkdir portfolio && cd portfolio && git init -q
mkdir docs
cat > docs/index.html <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Ada Lovelace</title>
<link rel="stylesheet" href="style.css"></head>
<body><h1>Ada Lovelace</h1><p>Analyst, metaphysician, programmer.</p></body>
</html>
EOF
printf 'body { font-family: Georgia, serif; max-width: 40em; margin: auto; }\n' > docs/style.css
touch docs/.nojekyll
git add . && git commit -q -m "Add portfolio site"
git remote add origin ~/github/ada.github.io.git && git push -q -u origin main
git ls-tree -r --name-only origin/maindocs/.nojekyll
docs/index.html
docs/style.css
.nojekyll tells Pages to serve the files as they are instead of running them through Jekyll (otherwise files and folders starting with _ are skipped).
Publishing a build on a gh-pages branch
When your site is generated (Vite, Hugo, Jekyll, a docs tool), you often keep the source on main and only the built files on a separate branch. An orphan branch starts with no history and no files:
git init -q --bare ~/github/calculator.git
mkdir calculator && cd calculator && git init -q
printf 'export const add = (a, b) => a + b;\n' > calc.js
echo "dist/" > .gitignore
git add . && git commit -q -m "Add calculator source"
git remote add origin ~/github/calculator.git && git push -q -u origin main
# pretend build step
mkdir -p dist && printf '<!DOCTYPE html><title>Calc</title><script src="app.js"></script>\n' > dist/index.html
printf 'console.log(2 + 3);\n' > dist/app.js
git switch -q --orphan gh-pages
ls
cp dist/* . && rm -rf dist
git add index.html app.js
git commit -q -m "Deploy site"
git push -q -u origin gh-pages
git switch -q main
echo "--- branches on the server:"
git ls-remote --heads origin
echo "--- files on gh-pages:"
git ls-tree --name-only origin/gh-pagesdist
--- branches on the server:
824416b3d8bafa0506f2b3d2044c88dab9b30c02 refs/heads/gh-pages
59e619c06760519fcd8c2a7e70c65da7b7ccba0a refs/heads/main
--- files on gh-pages:
app.js
index.html
git switch --orphan empties the tracked files, so ls only shows the untracked (ignored) dist/ folder. Switching back to main brings the source back. In practice a tool such as the gh-pages npm package or a GitHub Actions workflow automates these steps.
Deploying with GitHub Actions
The modern option is an Actions workflow that builds and deploys on every push to main:
# .github/workflows/pages.yml
name: Deploy to Pages
on:
push:
branches: [main]
permissions:
contents: read
pages: write
id-token: write
jobs:
deploy:
runs-on: ubuntu-latest
environment: github-pages
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- uses: actions/upload-pages-artifact@v3
with: { path: dist }
- uses: actions/deploy-pages@v4Note: Pages only serves static files. There is no server-side code (PHP, Node, databases); use JavaScript in the browser or a separate API. A custom domain can be added under Settings → Pages with a
CNAMEfile.
Test yourself
Which file must the published folder contain for the home page?
- README.txt
- index.html (or index.md)
- pages.json
- home.css
What is an orphan branch?
- A branch whose remote was deleted
- A branch that starts with no parent commit, unrelated to the rest of history
- A branch with conflicts
- A branch nobody has pushed
Can GitHub Pages run a PHP or Node.js backend?
- Yes
- No, it only serves static files
- Only for paid accounts
- Only with Jekyll