【问题标题】:Why is it necessary to lose untracked files when you set up github pages?为什么设置 github 页面时需要丢失未跟踪的文件?
【发布时间】:2011-06-30 18:45:44
【问题描述】:

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 github git-branch


    【解决方案1】:

    最好的选择,制作一个新的克隆并这样做。如果您绝对必须在您当前工作的 repo 中执行此操作,请尝试 git checkout --orphan gh-pages

    【讨论】:

    • 我不是马!狸猫或熊^^
    【解决方案2】:

    如果您删除git clean 命令并执行git add <files> 而不是git add .,则可以在没有根目录的情况下创建gh-pages 分支。这将是工作流程:

    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
    

    我认为他们以简单的方式表达方向并避免“花哨”。

    【讨论】:

      【解决方案3】:

      这是因为 git pages 用于 web 文件(例如 .html)而不是源文件。所以这些说明告诉你的是如何在没有所有源文件的情况下创建一个完全干净的分支(在正常行为中你不想这样做),只托管 web-serve-able-on-github 文件、html、 CSS,JavaScript。

      所以如果你想要干净,你可以这样做。如果你对完全肮脏没问题,你可以尝试简单地创建一个分支(git branch gh-pages)并推送它。我认为我以前做过,而且效果很好,尤其是当我开始处理存储库中的 Web 可服务内容时。

      【讨论】:

        【解决方案4】:

        问题是git clean git clean 从当前工作树中删除任何未跟踪的文件。

        您可以为未跟踪的文件设置.gitignore 文件,并保留-x 标志。您应该查看git-clean手册页。如果你没有unix系统:http://linux.die.net/man/1/git-clean

        【讨论】:

        • 大家好,谢谢大家的回答!他们都教会了我更多关于 git 的知识,我非常感谢。
        • 没问题@Kenneth McDonald,请不要忘记接受其中一个答案。
        猜你喜欢
        • 2018-07-20
        • 2021-08-20
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-28
        • 2020-11-21
        • 2020-02-22
        相关资源
        最近更新 更多