Git Orphan Branch Guide: Decouple Docs and GitHub Pages with a Clean History

Git tutorial - IT technology blog
Git tutorial - IT technology blog

1. The Wake-Up Call: When the Documentation Branch Dwarfed the Source Code

Around mid-last year, our team set up an internal documentation site using GitHub Pages for an ERP project. As a quick shortcut, someone ran git checkout -b gh-pages directly from main. The team then dumped the entire Docusaurus build output, hundreds of Retina screenshots, and several demo MP4 videos straight into this branch.

Six months into production, the fallout hit hard:

  • The repository ballooned to 1.2 GB. Onboarding new engineers became painful, with cloning taking over 8 minutes on the office network.
  • The Git history on the doc branch inherited more than 3,200 backend commits, database migration scripts, and bug fixes. Tracking down a documentation changelog felt like finding a needle in a haystack.
  • A staging environment configuration file was accidentally left behind on the documentation branch during a bad merge. Fortunately, the DevOps team caught it before anyone else discovered it.

A static documentation site only needs HTML, CSS, and JavaScript. It has no business carrying thousands of lines of legacy business logic or outdated database migration files.

2. Why Does Git Retain the Entire Commit History?

When you run git checkout -b new-branch or git switch -c new-branch, Git points the new branch’s HEAD reference straight at the parent branch’s current commit. Every subsequent commit simply builds on top of this parent chain.

This inheritance model works brilliantly for feature branches or hotfixes, but it backfires in scenarios like:

  • Hosting static sites via gh-pages (Docusaurus, VitePress, MkDocs).
  • Storing standalone graphic assets, UI kits, or technical specs.
  • Archiving release bundles or HTML test coverage reports from CI/CD pipelines.

At that point, the legacy history becomes pure technical debt. Every checkout or clone forces Git to pull down hundreds of megabytes of blob objects, even if all you need is to view a lightweight static webpage.

3. Three Common Approaches

Approach 1: Moving to a Dedicated Repository

Offload all documentation to a new repository, such as my-project-docs.

  • Pros: Absolute isolation between source code and documentation, plus independent access control.
  • Cons: Cumbersome repo management, fragmented issue tracking across two repositories, and extra consumption of private repo quotas on free GitHub plans.

Approach 2: Checking Out from the Root Commit

Locate the hash of the very first commit when Git was initialized, check it out, and branch off from there.

  • Pros: A significantly shorter history compared to branching off HEAD.
  • Cons: A manual and error-prone process. Most importantly, it still carries an initial dummy commit instead of giving you a truly clean slate.

Approach 3: Using git checkout –orphan

This is Git’s native command for creating a completely decoupled branch. It has no parent commits and initiates a brand-new root commit within the exact same repository.

4. Step-by-Step Guide to Setting Up an Orphan Branch

After testing all three methods, our team settled on the Orphan Branch. It keeps the repo slim while centralizing management in one place. After the split, our documentation bundle shrank to just 42 MB, and CI/CD deploy times plummeted from 3 minutes down to 35 seconds.

Step 1: Initialize the orphan branch

Open your terminal at the project root and run:

# Traditional approach:
git checkout --orphan gh-pages

# Or using the modern syntax (Git 2.23+):
git switch --orphan gh-pages

Git immediately switches you to the gh-pages branch. This branch has zero commits. However, all files from your previous branch will still linger in your working directory and staging area.

Step 2: Clear the staging area and working directory

Don’t panic when you see a long list of green files in git status. Simply run this command to wipe the slate clean:

# Remove all files from the staging area and working tree
git rm -rf .

This command only removes files from the current orphan branch. Your main branch and original codebase remain completely untouched. Run git status again, and you will see: No commits yet on branch gh-pages.

Step 3: Add documentation content and make the first commit

Now that your working directory is spotless, add your static files or copy over your static site build output:

# Create a sample index file for GitHub Pages
echo "# Project Documentation" > index.md
echo "Welcome to the technical documentation site!" >> index.md

# Stage and make the initial commit
git add index.md
git commit -m "docs: initial orphan commit for github pages"

Step 4: Safely push to the remote repository

When pushing the new branch to the remote for the first time, establish the upstream tracking:

git push -u origin gh-pages

A hard-learned lesson: Always double-check your active branch with git branch before running a push command. If you mistakenly run git push --force origin main while on an orphan branch, you could overwrite the entire primary codebase with an empty repository. Make sure to enable GitHub Branch Protection Rules on main to eliminate this risk entirely.

Step 5: Inspect the independent commit tree

Run a visual log command to verify that the new branch is completely decoupled:

git log --graph --oneline --all

In the graph view, the gh-pages branch appears as an isolated, standalone track with zero links back to main or develop. Your repository stays lightweight, organized, and effortlessly maintainable under a single roof.

Share: