git 在 Github Pages 上将子目录设置为网站根目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36782467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:06:26  来源:igfitidea点击:

Set subdirectory as website root on Github Pages

gitgithubserverwebservergithub-pages

提问by Oriol

I'm using Github Pagesto host & serve a static website.

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

The static website has the typical directory structure for an app:

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

|_ source
|_ build
   |_index.html
 .gitignore
 config.rb
 Gemfile
 ...
 README.MD

index.htmlis underbuild/, so I want to make that the default wwwpath.

index.html在 下build/,所以我想将其设为默认www路径。

So when users hit username.github.ioit renders the content within that subdirectory and yet it doesn't show "/build"/ on the URL, cause that's set as the root folder.

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

Notes:

笔记:

  • I don't have a custom domain nor planning to get one for this purpose. As you can see, I'm trying to leverage the default URL naming convention github provides.
  • Not using Jekyllnor the automatic page generator function.
  • 我没有自定义域,也不打算为此目的获得一个。如您所见,我正在尝试利用 github 提供的默认 URL 命名约定。
  • 不使用Jekyll也不使用自动页面生成器功能。

采纳答案by CodeWizard

There is a detailed gist with all the required steps.

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

The gist is here:
https://gist.github.com/cobyism/4730490

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



From the gist

从要点

Deploying a subfolder to GitHub Pages

Sometimes you want to have a subdirectory on the masterbranch be the root directory of a repository's gh-pagesbranch. This is useful for things like sites developed with Yeoman, or if you have a Jekyll site contained in the masterbranch 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 distdirectory from the project's .gitignorefile (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-pagesbranch 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.

将子文件夹部署到 GitHub Pages

有时您希望master分支上的子目录作为存储库gh-pages分支的根目录。这对于使用Yeoman开发的站点等内容非常有用,或者如果您的master分支中包含 Jekyll 站点以及您的其余代码。

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

第1步

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

第2步

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

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

第 3 步

使用 subtree push 将其发送到gh-pagesGitHub 上的分支。

git subtree push --prefix dist origin gh-pages

繁荣。如果您的文件夹没有被调用dist,那么您需要在上面的每个命令中更改它。



If you do this on a regular basis, you could also create a scriptcontaining 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

这使您可以键入以下命令:

git gh-deploy path/to/your/site

回答by Kpym

Since August 2016you can use /docssubfolder of the masterbranch for your sources.

自 2016 年 8 月起,您可以使用分支的/docs子文件夹作为master源。

So if you can tell to your site generator to use /docsin place of /buildyou are done (without subtree).

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

Note:As pointed out by @thislooksfun in the comment, this is valid only for project pages (like <username>.github.io/<projectname>), but not for user or organization pages (like <name>.github.io).

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

回答by roli roli

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

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

I solve it by running the commands below:

我通过运行以下命令来解决它:

  • git checkout --orphan gh-pagesGit Reference

  • git --work-tree build add --allGit Reference

  • git --work-tree build commit -m 'gh-pages'

  • git push origin HEAD:gh-pages --force

  • git checkout -f master

  • git checkout --orphan gh-pages Git 参考

  • git --work-tree build add --all Git 参考

  • git --work-tree build commit -m 'gh-pages'

  • git push origin HEAD:gh-pages --force

  • git checkout -f master

回答by RobLoach

push-dirwill do it:

push-dir会这样做:

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