【问题标题】:Set subdirectory as website root on Github Pages在 Github Pages 上将子目录设置为网站根目录
【发布时间】:2016-08-15 09:56:30
【问题描述】:

我正在使用Github Pages 来托管和提供静态网站。

静态网站具有应用的典型目录结构:

.
├ source/
├ build/
│ └ index.html
├ .gitignore
├ config.rb
├ Gemfile
┆ ...
└ README.MD

index.htmlbuild/ 下,所以我想将其设为默认的www 路径。

因此,当用户点击username.github.io 时,它会呈现该子目录中的内容,但它不会在 URL 上显示“/build/”,因为它被设置为根文件夹。

注意事项:

  • 我没有自定义域,也没有计划为此目的获得一个。如您所见,我正在尝试利用 github 提供的默认 URL 命名约定。
  • 未使用Jekyll 也未使用自动页面生成器功能。

【问题讨论】:

标签: git github server webserver github-pages


【解决方案1】:

有一个包含所有必需步骤的详细要点。

要点在这里:
https://gist.github.com/cobyism/4730490


从要点

将子文件夹部署到 GitHub Pages

有时您希望将 master 分支上的子目录作为存储库的 gh-pages 分支的根目录。这对于使用 Yeoman 开发的网站之类的东西很有用,或者如果您在 master 分支中与其余代码一起包含 Jekyll 网站。

为了这个例子,让我们假设包含您网站的子文件夹名为dist

步骤 1

从项目的 .gitignore 文件中删除 dist 目录(默认情况下 Yeoman 会忽略它)。

第二步

确保 git 知道您的子树(您网站所在的子文件夹)。

git add dist && git commit -m "Initial dist subtree commit"

第三步

使用子树推送将其发送到 GitHub 上的gh-pages 分支。

git subtree push --prefix dist origin gh-pages

轰隆隆。如果您的文件夹不名为 dist,那么您需要在上面的每个命令中更改它。


如果您定期执行此操作,您还可以 create a script 在您的路径中包含以下内容:

#!/bin/sh
if [ -z "$1" ]
then
  echo "Which folder do you want to deploy to GitHub Pages?"
  exit 1
fi
git subtree push --prefix $1 origin gh-pages

它可以让你输入如下命令:

git gh-deploy path/to/your/site

【讨论】:

  • 因此使用这个 URL 约定来代替:username.github.io/projectname。正确的?所以这需要将 repo 重命名为“projectname”,以便我可以使用项目页面设置 (help.github.com/articles/user-organization-and-project-pages),对吗?
  • 是的。或者您可以改用自定义域
  • 好的。我想这是唯一的解决方案。谢谢!
  • 经过进一步研究,没有办法完成我最初提出的要求。如果您希望它出现在 username.github.io 中,则需要将 build 文件夹的内容放入根目录。否则使用上面的解决方案。
  • gh-deploy 不再是有效命令。我的子文件夹是 Firebase 默认要求您托管的 public 文件夹。
【解决方案2】:

Since August 2016 你可以使用master 分支的/docs 子文件夹作为你的源代码。

因此,如果您可以告诉站点生成器使用 /docs 代替 /build,您就完成了(没有子树)。

注意:正如@thislooksfun 在评论中指出的那样,这仅对项目页面(如<username>.github.io/<projectname>)有效,但对用户或组织页面(如<name>.github.io)无效。

【讨论】:

  • 值得注意的是,这仅适用于非用户/组织网站。更具体地说,“要从 master 分支上的 /docs 文件夹发布站点的源文件,您必须有一个 master 分支,并且您的存储库不得遵循存储库命名方案 .github.io 或 .github。 io" (help.github.com/articles/…)
  • 另请注意,如果您的帐户下已经有另一个存储库<username>.github.io,那么/docs 将不起作用。仅在master 上直接查找 index.html。在此处查看页面顶部:help.github.com/articles/…
【解决方案3】:

不知何故,对我来说,接受的答案只在第一次运行。重新执行它会引发错误。

我通过运行以下命令解决了这个问题:

git checkout --orphan gh-pages
git --work-tree build add --all
git --work-tree build commit -m 'gh-pages'
git push origin HEAD:gh-pages --force
git checkout -f master

【讨论】:

  • 错误:src refspec HEAD 不匹配任何它告诉我这个
【解决方案4】:

为了让它适用于使用 public/ 文件夹的 Hugo 网站的 gh-pages 分支,这是我正在做的工作,虽然超级hacky,但可以完成工作:

注意:hugolanding 是您的 config.toml 所在的文件夹根目录,此脚本从脚本文件夹运行,您完全可以将其移至别处并更改该行。

#!/bin/bash
# move to root
cd ../hugolanding
# generate public content
hugo -D
# prep copy folder
rm -rf /tmp/public && mkdir -p /tmp/public
# copy out of git shit
cp -R public/* /tmp/public
# git yolo everything
git add -A 
git commit -m 'updates to public'
git push --recurse-submodules=on-demand
git checkout gh-pages
cd ..
cp -R /tmp/public/* .
git add -A 
git commit -m 'updated gh-pages'
git push --recurse-submodules=on-demand
echo "done"
git checkout main

【讨论】:

    【解决方案5】:

    push-dir 会做到的:

    npm install push-direxample
    push-dir --dir=build --branch=gh-pages
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2020-10-03
      • 2012-03-13
      • 2021-12-19
      • 2018-10-03
      相关资源
      最近更新 更多