GitHub Pages
Learn how to host and deploy static websites directly from your GitHub repositories using GitHub Pages.
GitHub Pages
GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files directly from a repository on GitHub, optionally runs the files through a build process, and publishes a website. It's a powerful way to showcase projects, documentation, or personal websites directly from your GitHub repositories.
Getting Started with GitHub Pages
- Free hosting for static websites
- Direct deployment from GitHub repositories
- Automatic builds for Jekyll sites
- Support for custom domains
- HTTPS encryption included
- Versioning through Git
GitHub Pages supports three types of sites:
- Project sites: Hosted from a specific repository
- User sites: Named
username.github.io
, showcases user content - Organization sites: Named
organization.github.io
, represents an organization
Setting Up GitHub Pages
- Create or navigate to your GitHub repository
- Go to repository Settings
- Scroll down to the "GitHub Pages" section
- Select a source branch (usually
main
orgh-pages
) - Choose a publishing folder (root
/
or/docs
) - Click "Save"
- Wait for your site to be published (URL will be displayed when ready)
For project sites, GitHub Pages is available in public repositories for free, and in private repositories if you have GitHub Pro or a team/organization plan.
Directory Structure
The content of your GitHub Pages site depends on how you configure it:
repository/
āāā .github/ # GitHub-specific files
āāā docs/ # If publishing from /docs
ā āāā index.html # Main page
ā āāā css/ # Stylesheets
ā āāā js/ # JavaScript files
āāā index.html # If publishing from root
āāā README.md # Repository information
Working with Themes
GitHub Pages provides built-in themes for quick setup:
- Go to repository Settings
- Scroll to the "GitHub Pages" section
- Click "Choose a theme"
- Select your preferred theme
- Commit the changes
When you select a theme, GitHub creates a _config.yml
file in your repository with the theme configuration.
Using Jekyll with GitHub Pages
GitHub Pages has built-in support for Jekyll, a static site generator:
- Automatic builds on GitHub
- Templating and layouts
- Blog-aware features
- Front matter for metadata
- Markdown support
repository/
āāā _config.yml # Site configuration
āāā _layouts/ # Page templates
ā āāā default.html # Default layout
āāā _posts/ # Blog posts
ā āāā 2023-01-01-post.md
āāā _data/ # Data files
āāā _includes/ # Reusable components
āāā _sass/ # SCSS partials
āāā assets/ # Static assets
āāā index.md # Home page
Advanced GitHub Pages Features
Custom Domains
- Purchase a domain from a domain registrar
- Go to your repository's Settings
- In the "GitHub Pages" section, enter your custom domain
- Configure your domain's DNS settings:
- For apex domains: Create A records pointing to GitHub's IP addresses
- For subdomains: Create a CNAME record pointing to
username.github.io
- Create a
CNAME
file in your repository with your domain name
Secure Your Site with HTTPS
GitHub Pages automatically secures custom domains with HTTPS:
- Ensure your DNS is correctly configured
- Check the "Enforce HTTPS" option in your GitHub Pages settings
- Wait for the certificate to be provisioned (can take up to 24 hours)
If your site uses custom plugins or build processes not supported by GitHub Pages, consider using GitHub Actions for more advanced deployment workflows.
Troubleshooting GitHub Pages
Common issues and solutions:
- 404 errors: Check if your repository is public and pages are enabled
- CSS not loading: Ensure paths are relative or use the
{{ site.baseurl }}
prefix - Build failures: Review build logs in the Actions tab
- Custom domain not working: Verify DNS settings and CNAME file
- Content not updating: Check if you've pushed to the correct branch