Sub topic 5
Publishing from a branch and folder
This chapter assumes you can clone, commit, and push with Git, that you have a GitHub repository, and that you can read basic HTML. You will publish a site by pointing GitHub Pages at a branch and a folder.
How branch publishing works
With branch publishing, GitHub Pages watches one branch of your repository. Each time you push to that branch, GitHub takes the files in the chosen folder and serves them as your site. You do not write a workflow file. GitHub runs a built-in deployment for you.
You choose two things:
- Branch: any branch in the repository, such as
mainorgh-pages. - Folder: either
/ (root)or/docs. These are the only two choices.
The folder you choose is the publishing root. The file index.html in that folder becomes the home page.
Add a first page
Create index.html in the folder you plan to publish. This minimal page is enough for a first deployment. The stylesheet link is relative, so it keeps working when the site is served from a project path.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My site</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello from GitHub Pages</h1>
</body>
</html>Put a style.css file next to it. Then commit and push the files to the branch you will publish.
Turn on publishing
- Open the repository on GitHub and go to Settings, then Pages.
- Under Build and deployment, set Source to Deploy from a branch.
- Choose the branch, for example
main. - Choose the folder, either
/ (root)or/docs, and select Save.
GitHub starts a deployment. When it finishes, the Pages settings show the site address. The first deployment can take a minute or two. Later pushes to the branch redeploy the site automatically.
You can follow each deployment in the Actions tab. Branch publishing shows up there as a run named "pages build and deployment", even though you wrote no workflow.
Custom domains and the CNAME file
If you add a custom domain to a branch-published site, GitHub records it in a file named CNAME at the root of the published folder. The file holds one line with the domain name and nothing else:
example.comIf you publish from /docs, the file lives at docs/CNAME. Do not delete it, or the domain setting disappears on the next deployment. Domain setup has its own chapter.
Jekyll runs by default
Branch publishing runs your files through Jekyll, a static site generator, before serving them. Plain HTML and CSS pass through unchanged in most cases. However, Jekyll skips files and folders whose names start with an underscore or a dot, and it treats files with front matter as templates to process.
To serve the files exactly as they are, add an empty .nojekyll file to the published folder:
touch .nojekyll
git add .nojekyll
git commit -m "Disable Jekyll processing"
git pushRun these commands inside the folder you publish. For /docs, that means docs/.nojekyll.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| 404 on the home page | No `index.html` in the chosen folder | Add `index.html` to the root or `docs` folder you selected |
| Changes do not appear | Pushed to a different branch than the one selected | Check the branch in **Settings**, then **Pages** |
| Files in an `_assets` folder are missing | Jekyll skips underscore folders | Add `.nojekyll` or rename the folder |
| `.nojekyll` has no effect | File is in the repository root, but you publish `/docs` | Move it into `docs` |
Exercise
Exercise
Your repository publishes from the main branch and the /docs folder. You add .nojekyll at the repository root and push, but a folder named _data inside docs is still missing from the site. Why, and what do you do?
Show answer
Only the contents of docs are the publishing root, so a .nojekyll file at the repository root is not seen. Jekyll still runs and skips the underscore folder. Create the empty file at docs/.nojekyll, commit, and push. The next deployment serves _data as it is.