Search code examples
githubgithub-pages

github site from gh-pages branch


I have GitHub site like example.github.io and want to load this site from the gh-pages branch! how can I do?

now I can't change branch because my repository name is example.github.io


Solution

  • When you create yourusername.github.io you have to commit directly to master. It's meant as a website (unlike a repo that has a website that hosts to yourusername.github.io/reponame/*.

    Since you already have a branch with the website, just merge it into the master branch. This can be done from the command line, the Github website or Github Desktop.


    The logic

    Example: You have a repo (username.github.io). You also have two repos set up with Github pages: repo-1 and repo-2.

    username.github.io is the root. You're free to do pretty much anything you want with it. Creating directories will be relative to the root of the domain. When you connect to the domain, it finds the files from the master branch. If you go into the repo settings for username.github.io, you'll see this:

    User pages must be built from the master branch

    You're locked to the master branch. You can still use other branches for adding features, but what the actual website consists of is what's on the master branch.

    The difference between the username.github.io repo and repo-1 or repo-2 is that username.github.io allows editing access to the directory root (e.g. http(s)://username.github.io/index.html) where as repo-1 and its connected pages would be at http(s)://username.github.io/reponame/index. Since the website is most likely wanted to stay out of the source itself, you can use an alternate branch to host it.

    If you go into settings for repo-1 or repo-2, you'll see this:

    enter image description here

    Here you can pick. If you have multiple branches, you can select a different one to host the pages. You can also select the option to use the /docs folder for the website.


    TL:DR; When using username.github.io, the master branch is the one that actually hosts the website. Think of it as the production branch. The others can't be accessed from the website