How to host MkDocs Project using GitHub Pages
This step-by-step guide will walk you through creating a documentation site with MkDocs and hosting it using GitHub Pages.
Prerequisites:
- Basic knowledge of Git and GitHub
- Python 3.7 or above installed
- Git installed
- A GitHub account
Step 1: Create a New MkDocs Project
-
Install MkDocs:
pip install mkdocs
-
Create a new project:
mkdocs new my-docs cd my-docs
-
Preview your site locally
Follow the link -mkdocs serve
http://127.0.0.1:8000
in your browser & you'll see the default homepage being displayed :
Step 2: Initialise Git Repository
-
Initialise a Git repo and commit files
git init git add . git commit -m "Initial MkDocs site"
-
Create a new repository on GitHub (e.g.,
my-docs-site
) - Link the remote and push:
git remote add origin https://github.com/YOUR_USERNAME/my-docs-site.git git branch -M main git push -u origin main
Step 3: Configure mkdocs.yml
- Add or update the following lines in
mkdocs.yml
site_name: My Docs theme: name: material
- Install Material theme (if used):
pip install mkdocs-material
Step 4: Build the Site
Generate the static files using:
mkdocs build
site/
directory containing the final HTML that looks like this:
Step 5: Deploy to GitHub Pages
-
Deploy using:
mkdocs gh-deploy
-
This command:
- Builds the site
- Pushes it to the gh-pages branch on your GitHub repo
Note
Make sure your GitHub repo has gh-pages enabled in settings.
Step 6: Enable GitHub Pages
-
Go to your GitHub repo’s Settings > Pages
-
Choose Branch: gh-pages
/ (root)
folder -
Save the changes
Step 7: Access Your Documentation Site
Your GitHub Pages hosted site will be available at:
https://YOUR_USERNAME.github.io/REPO_NAME/
https://charvi-rajput.github.io/my-docs-site/