GitHub Pages 02 ๐ Branch & Folder Publishing โ Configuring deployments from main, gh-pages, and /docs
GitHub Pages deployment is not a “one size fits all” configuration. While the repository acts as the container, the specific files served as your website are determined by the “Source” setting in your repository’s Pages dashboard. You can choose to serve your content from the root of a branch, a dedicated /docs folder, or an entirely separate branch dedicated to deployment artifacts.
Choosing the right deployment source depends on your development workflow. If you write static HTML files manually, serving from the root of the main branch is the simplest approach. If you are managing documentation alongside source code, the /docs folder pattern allows the site to live within your codebase without interfering with the root. For projects that require complex build stepsโsuch as React, Vue, or Svelte applicationsโdeploying from a dedicated branch or an automated build artifact is the standard approach to keep your source code clean and your deployment history distinct.
This chapter covers the three primary deployment configurations: root-branch deployment, the /docs folder convention, and the use of the gh-pages branch. We will walk through how these settings dictate your site’s URL and why mixing build artifacts with source code in the same folder can create maintenance headaches.
Key point: The “Source” setting in your repository’s GitHub Pages configuration acts as the pointer for the deployment engine; it tells GitHub exactly which branch and which directory (/ or /docs) to treat as the root of your live website.
Why X exists
The separation of concerns problem. When you build a modern frontend application, you often have a src/ directory filled with raw code and a dist/ or build/ directory filled with optimized, minified files. If you commit these built files to your main branch, it creates massive diffs, confuses code reviewers, and pollutes your commit history. GitHub Pages source configurations exist to isolate this output.
The “docs-as-code” requirement. Developers want documentation to live in the same repository as their source code to ensure that the API documentation version matches the software version. The /docs folder source allows you to keep your project root clean while hosting a dedicated documentation site right alongside your library, all served from a single repository.
The legacy vs. modern workflow evolution. The gh-pages branch is a long-standing convention where developers manually maintain a separate branch containing only the site files. While newer GitHub Actions workflows have automated this process, the gh-pages branch remains a vital configuration for teams that prefer manual control or legacy deployment pipelines.
The repository root clutter issue. Without folder-specific deployment, every single repository would require an index.html file in the root to function. This is problematic if you have a multi-purpose repository or a project that isn’t primarily a website. The folder source setting allows a repository to serve a website while still housing non-web scripts, tests, and configuration files in the root.
The trade-off. The trade-off between branch-based and folder-based deployment is primarily about maintenance overhead. Deploying from a branch is simple but can clutter your main commit history if you are committing build artifacts. Deploying from a folder is cleaner but requires careful path management so that relative links (like CSS and images) continue to function correctly in a sub-path.
a. Root folder from a branch
Deploying from the root of a branch (usually main or master) is the default behavior. GitHub treats the top-level directory of that branch as the web server’s root. If you have an index.html file at the top of your repository, that file is served immediately at https://<username>.github.io/<repo>/.
This method is best suited for static site generators that output to the root or for simple landing pages built with standard HTML/CSS. When configured, GitHub monitors the specified branch; whenever you push a change to any file in that branch, the site rebuilds.
# Repository structure for root deployment
.
โโโ index.html # Served at the root
โโโ css/
โ โโโ style.css
โโโ README.md # Not served, but present in repo
The simplicity here is an advantage, but it exposes your repository root to web requests. Any file in the root is technically accessible via URL, which is why sensitive configuration files should never be stored in the root of a repository designated for GitHub Pages deployment.
b. The /docs folder configuration
The /docs folder deployment is a specific GitHub Pages feature designed for documentation websites. When you select this source in the repository settings, GitHub ignores the root of the branch and looks specifically inside the /docs directory. This allows you to keep your project source code (e.g., src/, package.json, tests/) in the root and your website content isolated in one place.
This is the standard for projects like “Awesome Lists” or documentation sites generated by tools like MkDocs or Docusaurus. You simply change the Pages setting from “Root” to “/docs”.
# Repository structure for /docs deployment
.
โโโ src/ # Source code (not served)
โโโ package.json
โโโ docs/ # Deployment source
โ โโโ index.html # Served at the root
โ โโโ assets/
โโโ README.md
Using this method, if a user navigates to your site URL, they see the content of docs/index.html. You do not need to configure your web server to redirect or rewrite paths; GitHub handles this mapping automatically.
c. The dedicated gh-pages branch
The gh-pages branch is a specialized workflow where the branch only contains the files you want to host. It acts as a staging area for your website. This is historically how developers deployed Single Page Applications (SPAs) like React or Angular apps. You would build your app locally, switch to the gh-pages branch, copy the build artifacts, and push.
While GitHub Actions now automates this process (by building the site and pushing it to a branch for you), the configuration remains: you set the Pages source to the gh-pages branch.
# Branch separation strategy
# Main Branch (Development)
โโโ src/
โโโ package.json
# gh-pages Branch (Deployment)
โโโ index.html # The compiled React/Vue output
โโโ static/
โโโ main.js
This method is the cleanest way to avoid cluttering your development branch with compiled assets. However, it requires careful synchronization. If you forget to update the gh-pages branch after a change in main, your site will display stale content. This is why automated workflows are preferred over manual gh-pages management.
Complete Example Session
# ============================================
# PART 1: INIT THE REPO
# ============================================
mkdir project-demo
cd project-demo
git init
echo "<h1>Root Deployment</h1>" > index.html
git add . && git commit -m "Initial commit"
# ============================================
# PART 2: CONFIGURE ROOT DEPLOYMENT
# ============================================
# (In GitHub Settings > Pages)
# Select Branch: main
# Select Folder: / (root)
# ============================================
# PART 3: MOVE TO /DOCS PATTERN
# ============================================
mkdir docs
mv index.html docs/
echo "<h1>Docs Folder Deployment</h1>" > docs/index.html
git add docs/ && git commit -m "Move to docs"
# ============================================
# PART 4: CONFIGURE /DOCS SOURCE
# ============================================
# (In GitHub Settings > Pages)
# Select Branch: main
# Select Folder: /docs
# ============================================
# PART 5: CREATE GH-PAGES BRANCH
# ============================================
git checkout --orphan gh-pages
git rm -rf .
echo "<h1>Gh-Pages Branch Deployment</h1>" > index.html
git add index.html
git commit -m "Deploy to gh-pages"
git push origin gh-pages
# ============================================
# PART 6: CONFIGURE GH-PAGES SOURCE
# ============================================
# (In GitHub Settings > Pages)
# Select Branch: gh-pages
# Select Folder: / (root)
# ============================================
# PART 7: VERIFYING THE BUILD
# ============================================
# GitHub will show: "Your site is live at ..."
# Navigate to the URL to verify the content
# ============================================
# PART 8: THE .NOJEKYLL FILE
# ============================================
# Essential for non-Jekyll sites (React/Vue/Angular)
touch .nojekyll
git add .nojekyll
git commit -m "Add nojekyll"
git push
# ============================================
# PART 9: BRANCH SWITCHING
# ============================================
# Ensure your local git matches your GitHub setting
git checkout main
# (Any changes here won't affect gh-pages deployment)
# ============================================
# PART 10: AUTOMATION (CONCEPT)
# ============================================
# Future step: Create .github/workflows/deploy.yml
# This replaces manual gh-pages branch management
The parts covered initializing a repo, configuring the three main sources (root, docs, and branch), managing the .nojekyll file, and preparing for automation.
Quick Reference
Deployment Source Options
| Source | Structure | Use Case |
|---|---|---|
| Branch Root | main -> / | Simple sites, SSGs |
| Docs Folder | main -> /docs | Docs-as-code |
| gh-pages | gh-pages -> / | SPAs, Build Artifacts |
Workflow Comparison
| Strategy | Complexity | Cleanliness | Automation |
|---|---|---|---|
| Root | Low | Low | Git Push |
| Docs | Medium | High | Git Push |
| gh-pages | High | High | Actions/Manual |
Configuration Key
| File | Purpose | Required For |
|---|---|---|
.nojekyll | Skips Jekyll build | React/Vue/Angular |
_config.yml | Jekyll config | Jekyll sites |
.gitignore | Ignore build folders | Local development |
Best Practices
โ Do This:
# Create a .nojekyll file if not using Jekyll // โ
# Use /docs for documentation-heavy repositories // โ
# Use GitHub Actions to automate gh-pages updates // โ
# Keep your deployment branch/folder strictly clean // โ
# Use relative links for assets (./css/style.css) // โ
โ Don’t Do This:
# Push minified build files to the main branch // โ
# Use absolute root paths (/assets/img.png) // โ
# Forget to update the gh-pages branch manually // โ
# Serve sensitive files from the root directory // โ
# Mix source code and build output in the same folder // โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| 404 Page | Source path not set in Settings | Select Branch/Folder in Settings |
| Assets 404 | Absolute path usage | Use relative paths |
| Jekyll errors | Using React/Vue without .nojekyll | Add .nojekyll file |
| Build mismatch | Pushed to main, expected gh-pages | Align branch setting with git push |
| Folder not served | Not selected as ‘Source’ | Change Pages Settings to /docs |
Real-World Examples
1. React SPA
Repository uses gh-pages branch, updated by a GitHub Action that builds and deploys.
2. Docusaurus Docs
Repository uses /docs folder source on the main branch.
3. Personal Blog
Repository uses root deployment on the main branch with Jekyll.
4. Project README Site
Simple HTML landing page served from main branch root.
5. Legacy Component Library
Component demo files stored in /docs within the source repository.
6. Static Portfolio
Single-page CV served from the main branch root.
7. VuePress Site
Uses /docs folder as the source directory for content.
8. Documentation for Open Source
Maintains main for code and gh-pages for the generated docs site.
9. Hugo Website
Simple root deployment, building from Markdown in main.
10. Multi-Site Repository
Using different branches for different project demos.
Visual
Source Configuration Panel
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GitHub Pages Settings โ
โ โ
โ Build and deployment โ
โ Source: โ
โ [ Deploy from a branch โผ ] โ
โ โ
โ Branch: โ
โ [ main โผ ] / (root) โโ โ
โ โ โ
โ [ main โผ ] /docs โโโโ โ
โ โ
โ [ gh-pages โผ ] / (root) โโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Filesystem Organization
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ REPOSITORY ROOT โ
โ โ
โ /src (Code) โ
โ /dist (Build) โ
โ /docs (Docs) โโโบ Targeted by GitHub Pages โ
โ โ
โ index.html โโโโ Targeted by "Root" setting โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The gh-pages Workflow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DEVELOPMENT (main) DEPLOYMENT (gh-pages)
โ โ
โ - Raw Code - Compiled HTML โ
โ - Configs - Minified CSS โ
โ - Tests - Images โ
โ โ โ โ
โ โโโโโ Build Process โโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Root Deployment | Serve from / of any branch |
| Docs Deployment | Serve from /docs of any branch |
| gh-pages | Separate branch for artifacts |
| Config Location | Settings > Pages |
| Bypass Jekyll | .nojekyll file |
| Asset Links | Use relative paths |
| Automation | Preferred via GitHub Actions |
Key takeaways:
- Configuration dictates source. The GitHub Pages Settings page is the single point of truth for where your site content is pulled from.
- Root deployment is for simplicity. It is the default, but it exposes the entire root to the web.
- The
/docsfolder is for documentation. It allows you to maintain documentation and code in the same repository cleanly. - The
gh-pagesbranch is for artifacts. It keeps your development history separate from your compiled production site. - Always use
.nojekyllfor frameworks. If you are not using Jekyll, you must include a.nojekyllfile to prevent GitHub from trying to process your files incorrectly.
Remember: Deployment sources allow you to organize your repository in a way that makes sense for your project structure, rather than forcing your project to fit a rigid deployment structure. Whether you use the root, a specialized folder, or a dedicated branch, the key is consistency between your git workflow and your repository settings.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!