Why is it necessary to lose untracked files when you set up github pages?

Github has a feature where you can put up HTML pages. (Details here).

Anyway, I recently used this to put up the aforementioned page. The basics of the instructions to do so are:

  • git branch continually getting recreated on pull
  • Get list of files committed on git branch in last month from remote
  • How to do a merge in git without picking up any changes?
  • Why can't I use one ssh key on more than one github repo?
  • Git commit patches
  • Why do I need the “master” in git merge origin/master?
  • // In order to create a new root branch, first ensure that your working directory is clean by committing or stashing any changes. The following operation will lose any uncommitted files! You might want to run this in a fresh clone of your repo.
    $ cd /path/to/fancypants
    $ git symbolic-ref HEAD refs/heads/gh-pages
    $ rm .git/index
    $ git clean -fdx
    // After running this you’ll have an empty working directory (don’t worry, your main repo is still on the master branch). Now you can create some content in this branch and push it to GitHub. For example:
    $ echo "My GitHub Page" > index.html
    $ git add .
    $ git commit -a -m "First pages commit"
    $ git push origin gh-pages

    So that went fine; as advertised, my untracked files were wiped but I’d made a copy of the dir and just moved back what was necessary. Switching back and forth between branches (I use SmartGit) doesn’t seem to wipe untracked files.

    However, I’m interested in expanding my very basic knowledge of Git, and am wondering why it was necessary to wipe untracked files the first time gh-pages was set up. I would have thought it would be possible to set up the gh-pages branch, add and commit the html file to it and push it, all without affecting untracked files. And then just switch back to the original branch.

  • TFS2013 - Clone build definition for team project using git
  • How to recover from an unwanted merge of meta/config?
  • Visual studio 2010 git extention manage remotes unable to connect
  • subtree naming convention in git
  • Git: Undo local changes since failed merge
  • How to restore a corrupted git repository?
  • 4 Solutions collect form web for “Why is it necessary to lose untracked files when you set up github pages?”

    Best bet, make a new clone and do it in that. If you absolutely must do it in the repo you’re currently working in, try git checkout --orphan gh-pages

    You can create the gh-pages branch without the root directory if you drop the git clean command and do git add <files> instead of git add .. This would be the workflow:

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

    I think they express the directions the way they do for the sake of simplicity and avoiding “fanciness”.

    It’s because git pages is for web files (e.g. .html) instead of source files. So what those instructions are telling you is how to create a totally clean branch (which in normal behavior you don’t want to do) without all the source files, for hosting only web-serve-able-on-github files, html, css, javascript.

    So if you want clean, you can do it that way. If you’re okay with totally dirty, you can probably try simply creating a branch (git branch gh-pages) and pushing that. I think that I’ve done that before and it worked just fine, especially when I was dealing with just web-servable stuff in the repository to begin with.

    The Problem was the git clean
    git clean removes any untracked file from the current working tree.

    You could set up .gitignore files for the untracked files and leave the -x flag away. You should have a look at the manpage for git-clean. If you don’t have a unix system: http://linux.die.net/man/1/git-clean

    Git Baby is a git and github fan, let's start git clone.