git - 为什么在设置 github pages 时需要丢失未跟踪的文件?

标签 git github git-branch

Github 有一个功能,您可以在其中放置 HTML 页面。 (Details here)。

无论如何,我最近用它来建立上述页面。这样做的基本说明是:

// 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

一切顺利;正如所宣传的那样,我未跟踪的文件已被删除,但我已经复制了一份目录,只是移回了必要的部分。在分支之间来回切换(我使用 SmartGit)似乎不会删除未跟踪的文件。

但是,我有兴趣扩展我对 Git 的基本知识,并且想知道为什么在第一次设置 gh-pages 时必须删除未跟踪的文件。我原以为可以设置 gh-pages 分支,将 html 文件添加并提交给它并推送它,所有这些都不会影响未跟踪的文件。然后切换回原来的分支。

最佳答案

最好的选择,制作一个新的克隆并在其中进行。如果您绝对必须在您当前使用的存储库中执行此操作,请尝试 git checkout --orphan gh-pages

关于git - 为什么在设置 github pages 时需要丢失未跟踪的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539589/

相关文章:

git - 我们应该在 merge 到 master 后删除远程 Git 分支吗?

GitHub - 致命 : could not read Username for 'https://github.com' : No such file or directory

git - 如何仅在更改特定文件的提交上运行 git bisect?

ruby - 如何将项目 readme.md 读入 user.github.com 站点?

git - 我可以将两个不同版本的 cakephp 放入同一个 Git 存储库中吗?使用不同的 Git 分支?

git - 如何将原始 GitHub 项目的分支克隆到您的复刻中?

GitHub token - 限制访问

github如何 checkout 生产分支

git - 如何将推送恢复到主分支,然后再次从新分支推送相同的更改?

git - 如何找出git分支中更改了哪些文件(不是两个分支之间的区别)