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.