GitHub上部署Hexo网站

Hexo建站参见build-site-record-hexo

1. 建立远程GitHub连接

本地Git客户端

Hexo建站中提到Windows下安装Git客户端msysgit,安装完成后即可使用Git Bash进命令操作

生成新的SSH Key

在GitBash中

ssh-keygen -t rsa -C "邮件地址@youremail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):

在GitHub中添加生成的SSH Key 参见add-your-ssh-key-to-your-account

最后验证:

ssh -T git@github.com

详细设置可以参考: GitHub Help - Generating SSH Keys

遇到的报错处理

  • Could not open a connection to your authentication agent 错误, win7 下 使用
eval $(ssh-agent)
  • 在ssh -vT git@github.com 报 port 22: Attempt to connect timed out without establishing a connection

    需要 编写config文件在.ssh/目录下

Host github.com
User git
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

2. Github Pages

个人主页

也就是库的名称为yourname.github.io的主页,页面文件应当在master分支下,文件结构可以参考我的主页,也就是应当以HTML文件为主,是没有Markdown文件的。

主页源码可存储在新分支src下,与master独立

git init 
git branch  src 
git checkout src

 git add .
 git commit
 git remote add origin https://github.com/yourname/yourname.github.io.git
 git push origin src

node_modules和其他用于hexo发布的文件可以不用上传,添加目录或文件到.gitignore中

项目主页

也就是库名不是yourname.github.io的主页,页面文件应当在gh-pages分支下,文件结构与个人主页基本一致,同样没有Markdown文件。

只有yourname.github.io下master可以发布page
其他项目必须在 gh-pages分支下

建立 gh-pages分支,会识别为网页路径 在_config.yml的deploy下 branch 为gh-pages即可

deploy:
 type: git
 repo: https://github.com/username/repo.git
 branch: gh-pages

master 分支存储hexo源文件

使用命令git add .,将所有文件提交到缓存区。 使用命令git commit -m "add all files" ,将这些文件提交到本地仓库。 使用命令git push origin master,将本地仓库的改动推送到github仓库。

yourname.github.io/repo下访问项目网页,需要配置_config.yml下的路径

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://uuangian.github.io/repo
root: /repo/