Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
GitHub Pages
Next article icon

GitHub Pages

Last Updated : 06 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To simplify the process of deploying websites, blogs, portfolios, and documentation without the need for complex configurations, GitHub pages are used. GitHub Pages is a free, user-friendly static site hosting service that integrates directly with GitHub repositories. They solve hosting and deployment issues, making them an essential tool for developers.

What are GitHub Pages?

GitHub Pages is a feature on GitHub that lets you host static websites right from the GitHub repository. It's good for hosting personal, project, or organization pages and is free with support for custom domains. This means a very effortless way to share your work or personal projects with the world.

  • Free Hosting: GitHub Pages is free to use for both personal and project sites.
  • Ease of Setup: No complex configurations are needed; simply push your site to a GitHub repository.
  • Version Control: Since it’s tied to Git, you have full control over versioning and collaborative edits.
  • Custom Domain Support: GitHub Pages allows you to connect your own custom domain easily.
  • Built-in HTTPS: Securing your site with HTTPS is simple and free.

Types of GitHub Pages Sites

  1. User/Organization Sites: These sites are tied to your GitHub username or organization name and are hosted at username.github.io.
  2. Project Sites: These sites are tied to a specific repository and are hosted at username.github.io/repository.

Deployment Methods to GitHub Pages

There are various ways to deploy a project to GitHub Pages, depending on your project's nature and your preference for handling it post-deployment.

Basic Code for deployment

JavaScript
// src/app/js import './App.css';  function App() {     return (         <div className="App">             <h1>Welcome to github pages</h1>             <h3>this is was published using github pages</h3>         </div>     ); }  export default App; 

1. Deploying a Static Site from the main or master Branch

This is the simplest approach, where your static site is hosted directly from the root of your main or master branch.

Step 1: Navigate to the repository settings

In the "GitHub Pages" section, select the main or master branch as the source and save changes.

cd my-project

Step 2: Initialize Git repository and commit files

git init git add . git commit -m "Initial commit"

Step 3: Create a new repository on GitHub and push your code

git remote add origin https://github.com/username/my-project.git git push -u origin main

Now, enable GitHub Pages in the repository settings.

GitHub-Pages
GitHub Pages Settings

Output

After completing the above steps, your site will be live at https://username.github.io/my-project/.

OutPut
Deploying a Static Site from the main or master Branch

2. Deploying from a gh-pages Branch

This method involves using a dedicated gh-pages branch for GitHub Pages, allowing you to keep your main development branch separate from the deployment branch.

Step 1: Create and switch to the gh-pages branch

git checkout --orphan gh-pages

Step 2: Add your static files

git add . git commit -m "Deploy site to GitHub Pages"

Step 3: Push the branch to GitHub

git push origin gh-pages

Output

Your site will be live at https://username.github.io/my-project/.

OutPut
Deploying from a gh-pages Branch

3. Deploying with GitHub Actions

GitHub Actions can automate the deployment process, especially useful for projects that require a build step (like React or Next.js).

Step 1: Create a .github/workflows directory

mkdir -p .github/workflows

Step 2: Create a deploy.yml file in the .github/workflows folder

touch .github/workflows/deploy.yml

Step 3: Add the following workflow configuration

JavaScript
# .github/workflows/deploy.yml  name: Deploy to GitHub Pages  on:     push:         branches:             - main  jobs:     build:         runs-on: ubuntu-latest         steps:             - uses: actions/checkout@v2             - name: Set up Node.js               uses: actions/setup-node@v2               with:                   node-version: "14"             - run: npm install             - run: npm run build             - name: Deploy to GitHub Pages               uses: peaceiris/actions-gh-pages@v3               with:                   github_token: ${{ secrets.GITHUB_TOKEN }}                   publish_dir: ./build 

Step 4: Commit the Workflow File

git add .github/workflows/deploy.yml
git commit -m "Add GitHub Actions workflow for deployment"
git push origin main

After you push the deploy.yml file to the main branch, GitHub Actions will automatically run the workflow. You can monitor its progress in the Actions tab of your GitHub repository.

Your site will be live at https://your-username.github.io/my-app.

Output

OutPut

4. Deploying a React or Next.js Project

Step 1: For this method you have to update your package.json file

{     "name": "my-app",     "version": "0.1.0",     "private": true,     "homepage": "https://your-username.github.io/my-app",     "dependencies": {         "react": "^17.0.2",         "react-dom": "^17.0.2",         "react-scripts": "4.0.3"     },     "scripts": {         "start": "react-scripts start",         "build": "react-scripts build",         "test": "react-scripts test",         "eject": "react-scripts eject"     } }

Step 2: Build the project

npm run build

Step 3: Install gh-pages and deploy

npm install gh-pages --save-dev
npx gh-pages -d build

Output

Your React or Next.js site will be live at https://username.github.io/my-project/.

OutPut
Deploying a React or Next.js Project

Advantages of Using GitHub Pages

Pros

Cons

Free hosting

Limited to static content

Easy integration with GitHub

No server-side processing

Custom domains available

Limited to public repositories

Supports Jekyll for dynamic sites

Not suitable for large-scale apps

Version control built-in

Limited customization options

Best Practices for Securing Content on GitHub Pages

  • Use Strong Passwords and Two-Factor Authentication (2FA): Ensure your GitHub account is protected with a strong password and 2FA to prevent unauthorized access.
  • Keep Repository Private (if needed): For sensitive projects, consider keeping your repository private. This restricts access to your code and content to authorized users only.
  • Limit Access to Collaborators: Be selective about who has write access to your repository. Only add collaborators who need to contribute to the site.
  • Use Content Security Policy (CSP): Implement a Content Security Policy (CSP) in your site's HTML headers to protect against cross-site scripting (XSS) attacks.
  • Regularly Update Dependencies: Keep all dependencies (e.g., libraries, frameworks) up to date to ensure your site isn’t vulnerable to known security issues.

Next Article
GitHub Pages

R

rameshreddym7che
Improve
Article Tags :
  • Web Technologies
  • Git
  • GitHub

Similar Reads

    GitHub REST API
    The GitHub REST API allows developers to interact with GitHub programmatically, enabling you to manage repositories, handle issues, automate workflows, and integrate GitHub with other tools and platforms. Whether you're building an application, automating repetitive tasks, or just curious about how
    4 min read
    Issues in GitHub
    GitHub is more than just a platform for hosting code; it’s a powerful tool for managing projects and collaborating with teams. One of the key features that provides this is the Issues system. GitHub Issues helps developers track bugs, enhancements, and tasks, ensuring that projects stay organized an
    4 min read
    What is GitHub?
    For collaborative software development, version control systems have become an essential part of the development process. GitHub is one of the most popular platforms for version control and collaborative coding, and it plays an important role in how developers work together, manage their code, and c
    6 min read
    What is GitHub Labels?
    GitHub Labels are designed to help developers manage and organize issues and pull requests within their repositories. Labels act as tags that can be applied to issues and pull requests, providing a way to categorize, prioritize, and filter them based on various criteria. This article will explore wh
    4 min read
    GitHub Workflows
    GitHub Workflows are a powerful feature of GitHub Actions that automate tasks such as building, testing, and deploying your code directly from your GitHub repository. Workflows are highly customizable, allowing you to create automated processes that fit your specific project needs. This article prov
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences