新的系统上恢复 Hexo 博客项目并继续更新

一、安装必要环境

首先安装以下组件:

  1. Node.js(推荐 LTS 版本)
    ①官网:https://nodejs.org
    ②或者直接使用Google Firebase Studio来直接构建nodejs

  2. Hexo CLI 安装

1
npm install -g hexo-cli
  1. Git 安装
    官网:https://git-scm.com (本地环境需要安装,直接使用Google Firebase Studio不需要安装可以忽略)

二、完成GitHub账户认证

两种认证GitHub认证方式:
🅰️https方式 🅱️ssh方式

一、https格式:

  1. 修改 _config.yml 使用 https 地址
    在你的 Hexo 根目录(_config.yml 文件)中找到:
1
2
3
4
deploy:
type: git
repo: https://github.com/yourusername/yourusername.github.io.git
branch: main # 或 master,与你的 GitHub Pages 分支一致
  1. 连接GitHub命令
1
2
git config --global user.name "sunou"
git config --global user.email "sunou@example.com"

二、ssh格式:

  1. 修改 _config.yml 使用 SSH 地址
    在你的 Hexo 根目录(_config.yml 文件)中找到:
1
2
3
4
deploy:
type: git
repo: git@github.com:你的GitHub用户名/你的仓库名.git
branch: main # 或 master,与你的 GitHub Pages 分支一致

⚠️ 不要用 HTTPS!必须是 git@github.com:… 这种 SSH 格式。

例如:

1
2
3
4
deploy:
type: git
repo: git@github.com:yourname/yourname.github.io.git
branch: main

✅ 2.生成 SSH 密钥并添加到 GitHub
1️⃣ 生成 SSH 密钥(如果还没生成):

1
ssh-keygen -t ed25519 -C "your@email.com"

一路按回车,生成密钥对(默认保存在 ~/.ssh/id_ed25519

公钥为 id_ed25519.pub,私钥为 id_ed25519

2️⃣ 查看公钥内容:

1
cat ~/.ssh/id_ed25519.pub

3️⃣ 添加到 GitHub:
登录 GitHub
打开页面:https://github.com/settings/keys

点击 New SSH key

Title 可写 Hexo SSH

Key 填入上一步复制的内容

保存

✅ 3.测试 SSH 是否连接成功

1
ssh -T git@github.com

如成功会提示:

1
Hi devosv! You've successfully authenticated, but GitHub does not provide shell access.

如果失败,请检查是否:

添加了正确的公钥

GitHub 用户名一致

代理软件未拦截 SSH

✅ 4.重新部署 Hexo 博客

1
2
3
hexo clean
hexo g
hexo d

此时 Hexo 会用 SSH 自动推送到 GitHub,不会再要求密码或 Token。

三、从 GitHub 克隆博客源码

在 GitHub 上找到之前用于备份 Hexo 项目的源码仓库,比如:

https://github.com/yourusername/hexo-blog.git

然后在本地新建文件夹并克隆备份的Hexo项目源码到本地文件夹:

1
2
3
git clone https://github.com/yourusername/hexo-blog.git
cd hexo-blog
npm install

四、恢复主题

如果 themes 文件夹中没有主题,需要手动重新安装。例如使用默认主题 Anzhiyu:(前提是先要移动到本地新建Hexo备份文件夹themes文件夹下)

1
git clone -b main https://github.com/anzhiyu-c/hexo-theme-anzhiyu.git themes/anzhiyu

五、本地运行博客进行测试

执行以下命令:

1
2
3
hexo clean
hexo g
hexo s

然后访问本地地址:http://localhost:4000,确认博客正常恢复。

六、配置部署到 GitHub Pages

确保 _config.yml 中添加了部署信息:

1
2
3
4
deploy:
type: git
repo: https://github.com/yourusername/yourusername.github.io.git
branch: main

安装部署插件:

1
npm install hexo-deployer-git --save

部署命令:

1
hexo clean && hexo g && hexo d

七、备份 Hexo 源码到 GitHub 仓库

除了部署网站,我们也应该备份源码(文章、配置等):

  1. 创建 GitHub 源码仓库,例如:

https://github.com/yourusername/hexo-blog.git

  1. 本地备份命令:

假设你的 Hexo 项目已经存在在本地(比如路径在 D:\HexoBlog),操作如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd D:\HexoBlog

# 初始化 git 仓库(如果还没执行过)
git init

# 添加所有文件到版本控制
git add .

# 提交一次
git commit -m "初始化 Hexo 博客源码备份"

# 关联远程仓库(替换成你自己的)
git remote add origin https://github.com/yourusername/hexo-blog.git

# 推送到 GitHub(首次建议强制推 main 分支)
git branch -M main
git push -u origin main
  1. 之后更新文章后继续备份:
1
2
3
git add .
git commit -m "更新博客内容"
git push

建议使用 .gitignore 忽略如下内容:

1
2
3
4
5
node_modules/
public/
.deploy_git/
.DS_Store
db.json

遇到问题:例如遇到推送失败使用

1
2
git config --global http.proxy http://127.0.0.1:10808
git config --global https.proxy http://127.0.0.1:10808

更新插件和hexo本体命令

1
2
3
npm update
npm install hexo@latest --save
hexo clean && hexo g && hexo s