| | |

GitHub Pages 02 ๐Ÿ“„ Branch & Folder Publishing โ€” Configuring deployments from main, gh-pages, and /docs

โฑ๏ธ Estimated read: 10 min | |

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

SourceStructureUse Case
Branch Rootmain -> /Simple sites, SSGs
Docs Foldermain -> /docsDocs-as-code
gh-pagesgh-pages -> /SPAs, Build Artifacts

Workflow Comparison

StrategyComplexityCleanlinessAutomation
RootLowLowGit Push
DocsMediumHighGit Push
gh-pagesHighHighActions/Manual

Configuration Key

FilePurposeRequired For
.nojekyllSkips Jekyll buildReact/Vue/Angular
_config.ymlJekyll configJekyll sites
.gitignoreIgnore build foldersLocal 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

PitfallWhy It HappensFix
404 PageSource path not set in SettingsSelect Branch/Folder in Settings
Assets 404Absolute path usageUse relative paths
Jekyll errorsUsing React/Vue without .nojekyllAdd .nojekyll file
Build mismatchPushed to main, expected gh-pagesAlign branch setting with git push
Folder not servedNot 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

ItemValue
Root DeploymentServe from / of any branch
Docs DeploymentServe from /docs of any branch
gh-pagesSeparate branch for artifacts
Config LocationSettings > Pages
Bypass Jekyll.nojekyll file
Asset LinksUse relative paths
AutomationPreferred 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 /docs folder is for documentation. It allows you to maintain documentation and code in the same repository cleanly.
  • The gh-pages branch is for artifacts. It keeps your development history separate from your compiled production site.
  • Always use .nojekyll for frameworks. If you are not using Jekyll, you must include a .nojekyll file 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.

Was this helpful?


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!