Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created March 28, 2012 15:37
Show Gist options
  • Select an option

  • Save ramnathv/2227408 to your computer and use it in GitHub Desktop.

Select an option

Save ramnathv/2227408 to your computer and use it in GitHub Desktop.
Creating a clean gh-pages branch

Creating a clean gh-pages branch

This is the sequence of steps to follow to create a root gh-pages branch. It is based on a question at SO

cd /path/to/repo-name
git symbolic-ref HEAD refs/heads/gh-pages
rm .git/index
git clean -fdx
echo "My GitHub Page" > index.html
git add .
git commit -a -m "First pages commit"
git push origin gh-pages

Why do we need to do all this, instead of just calling git branch gh-pages. Well, if you are at master and you do git branch gh-pages, gh-pages will be based off master.

Here, the intention is to create a branch for github pages, which is typically not associated with the history of your repo (master and other branches) and hence the usage of git symbolic-ref. This creates a "root branch", which is one without a previous history.

Note that it is also called an orphan branch and git checkout --orphan will now do the same thing as the git symbolic-ref that was being done before. Check out this question on SO2 as well.

@MJ029

MJ029 commented Feb 3, 2018

Copy link
Copy Markdown

Tanx Works for me.

@glauberm

Copy link
Copy Markdown

Be careful with git clean -fdx, it will wipe out the files of the folder.

@mikong

mikong commented May 12, 2019

Copy link
Copy Markdown

Alternatively:

$ git checkout --orphan gh-pages

# preview files to be deleted
$ git rm -rf --dry-run .
# actually delete the files
$ git rm -rf .

Then create index.html, commit and push.

Based on Github Help pages (https://help.github.com/en/articles/creating-project-pages-using-the-command-line), but for gh-pages and with a dry run.

@dirien

dirien commented Jun 10, 2021

Copy link
Copy Markdown

Thank you! Perfect :)

@maggiben

Copy link
Copy Markdown

Nice thanks !

@jeremejazz

Copy link
Copy Markdown

The alternative approach looks simplier. Might try that out as well. Thank you @mikong !

@amazingrofa

Copy link
Copy Markdown

I do not know what to do..

@Designbrainy

Copy link
Copy Markdown

Here's an easier way i found:
cd your\project\current\directory
git init
git add .
git commit -m "Initial portfolio release"
git branch -M main
git remote add origin https://github.com/yourusername/yourrepo.git
git push -u origin main

@Designbrainy

Copy link
Copy Markdown

Be careful with git clean -fdx, it will wipe out the files of the folder.

It actually wiped out my files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment