在 Github 页面上将子目录设置为网站根目录

Set subdirectory as website root on Github Pages

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

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

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

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

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

备注:

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

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


从要点

Deploying a subfolder to GitHub Pages

Sometimes you want to have a subdirectory on the master branch be the root directory of a repository’s gh-pages branch. This is useful for things like sites developed with Yeoman, or if you have a Jekyll site contained in the master branch alongside the rest of your code.

For the sake of this example, let’s pretend the subfolder containing your site is named dist.

Step 1

Remove the dist directory from the project’s .gitignore file (it’s ignored by default by Yeoman).

Step 2

Make sure git knows about your subtree (the subfolder with your site).

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

Step 3

Use subtree push to send it to the gh-pages branch on GitHub.

git subtree push --prefix dist origin gh-pages

Boom. If your folder isn’t called dist, then you’ll need to change that in each of the commands above.


If you do this on a regular basis, you could also create a script containing the following somewhere in your path:

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

Which lets you type commands like:

git gh-deploy path/to/your/site

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

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

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

push-dir 会做:

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

Somehow for me, the accepted answer only runs the first time. Re-doing it throws errors.

我通过 运行 下面的命令解决了它:

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

为了使它适用于使用 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