Sub topic 3
Setting up the repository and your first page
This chapter assumes you can clone, commit, and push with Git, that you have a GitHub account, and that you can read basic HTML and CSS. You will create a repository, add a minimal page and stylesheet, and push them so the next chapters can publish them.
Choose the repository name
The repository name decides what kind of site you get. The name also fixes the site's URL, so choose it first.
| Repository name | Site type | URL |
|---|---|---|
| `USERNAME.github.io` | User site | `https://USERNAME.github.io/` |
| Any other name, such as `notes` | Project site | `https://USERNAME.github.io/notes/` |
For a user site, the name must match your account name exactly, followed by .github.io. Organization sites follow the same rule with the organization name. A project site can use any repository name, and the name becomes the first path segment of the URL.
The rest of this chapter uses a project site named notes. The steps are the same for a user site.
Create and clone the repository
Create an empty repository named notes on GitHub. Leave out the README, so the first push is simple. Then clone it:
git clone https://github.com/USERNAME/notes.git
cd notesPages serves public repositories on a free account. Private repositories need a paid plan, so use a public repository while you learn.
Decide where the site files live
Pages publishes from one location, which is either the repository root or a folder you choose. The next chapters cover how to choose that location. For now, put the files at the repository root. The root layout is the simplest to debug.
notes/
├── index.html
└── style.cssWhen a visitor requests a directory URL, Pages looks for index.html in that directory. The file name must be exactly index.html, all lowercase. A request for /notes/ returns the root index.html.
Write the first page
Create index.html:
<!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>The link href="style.css" is relative, with no leading slash. On a project site the page lives at /notes/, so the browser resolves this link to /notes/style.css. A link written as href="/style.css" would point at the account root and fail. Chapter 7 covers this problem in depth.
Now create style.css:
body {
font-family: system-ui, sans-serif;
max-width: 40rem;
margin: 2rem auto;
color: #222;
}
h1 {
color: #0b5cad;
}Commit and push
git add index.html style.css
git commit -m "Add first page"
git push origin mainIf your default branch has a different name, such as master, push that branch instead. You can check the name with git branch --show-current.
You can check the page locally before it goes live. Open index.html in a browser, or serve the folder with python3 -m http.server 8000 and visit http://localhost:8000/. The local server serves the site at the root, so it hides path problems that only appear under /notes/.
Check your work
After the push, open the repository on GitHub and confirm these points:
index.htmlis at the top level, not inside a subfolder.- The file name is lowercase and has the
.htmlextension. Pages serves files from a case-sensitive file system, soIndex.htmlis a different file. style.csshas the same spelling and case as thehrefvalue.
Your files are now in the repository, but the site is not live yet. GitHub serves nothing until you pick a publishing source in the repository settings. The next chapter covers that choice.
Exercise
You create a repository named portfolio under the account maria-dev and put index.html at its root. What URL will the site use, and what would you rename the repository to if you wanted it at https://maria-dev.github.io/?
Show answer
The project site URL will be https://maria-dev.github.io/portfolio/. To serve it at the account root, rename the repository to maria-dev.github.io, which makes it a user site.
Exercise
A page at /notes/ loads, but its styling is missing. The HTML contains <link rel="stylesheet" href="/style.css">. What is wrong, and what is the fix?
Show answer
The leading slash makes the path absolute, so the browser requests /style.css at the account root instead of /notes/style.css. Change it to the relative path href="style.css".