Academy · Intermediate · 15 min
Git and GitHub Pages: publish a website for free
Learn commits, branches and how to put a static website online with GitHub Pages at no cost.
Git records the history of your files. GitHub stores your repository online, and GitHub Pages can serve a static website from it for free. GM itself is published this way.
git init
git add .
git commit -m "First version"
git branch -M main
git remote add origin https://github.com/YOUR-NAME/YOUR-NAME.github.io.git
git push -u origin main
Steps
- Create a repository named
your-username.github.io. - Put an
index.htmlin it and push as above. - In the repository, open Settings, then Pages, and choose the
mainbranch. - Your site appears at
https://your-username.github.iowithin a minute or two.
Good habits
- Commit small changes with clear messages.
- Run
git pull --rebasebefore you push if others (or bots) also commit. - Never commit passwords, API keys or private files. Add them to
.gitignore. - Turn on two-factor authentication for your GitHub account.
Common mistakes and practice
- Committing passwords, API keys or private files. Keep secrets out of the repository and add them to
.gitignore. - Linking to
Page.htmlwhen the file ispage.html. GitHub Pages file names are case sensitive. - Expecting a change to appear instantly. A push can take a minute or two to publish, and the browser may show a cached copy.
Practice: Create a repository with an index.html, publish it with GitHub Pages, then change the heading, commit and push, and confirm the live page updates.
Before you publish your first site
Publishing with GitHub Pages is free and fits small sites well. Before you make a page public, check that it contains nothing private and that the links and images work.
Commit small changes often, with a short message that says what you changed. If something breaks, you can go back to an earlier version.
Practical tips
- Never commit passwords, API keys or private documents.
- Use relative links so the site works at its published address.
- Add a README so visitors know what the project is.
- It can take a minute or two for a new push to appear on the live site.