Hexo + GitHub + Vercel 多电脑使用指南
这套流程适用于在工作电脑、家用电脑之间切换使用 Hexo,并让 Vercel 自动发布网站。
一、整体流程
1 | 本地电脑修改 Hexo 源码 |
以后不需要执行 hexo deploy,Vercel 会负责发布。
二、项目中应该提交什么
应提交到 GitHub 的内容:
1 | source/ |
不应提交:
1 | node_modules/ |
.gitignore 至少应包含:
1 | node_modules/ |
三、首次上传到 GitHub
在 Hexo 项目根目录执行。远程地址必须是纯 URL,不要包含 Markdown 的 [] 或 ():
1 | git init |
如果提示 remote origin already exists,不要再次添加,先修正地址:
1 | git remote set-url origin https://github.com/你的用户名/你的仓库.git |
正确输出应类似:
1 | origin https://github.com/你的用户名/你的仓库.git (fetch) |
如果出现 src refspec main does not match any,通常是当前分支不是 main 或还没有提交。按以下顺序处理:
1 | git add . |
如果出现 Failed to connect to github.com:443,这是网络、代理或公司防火墙问题。可以检查:
1 | curl -I https://github.com |
确认不需要代理时可以清除错误代理:
1 | git config --global --unset http.proxy |
也可以切换手机热点后重试 git push。
四、Vercel 配置
在 Vercel 中选择 Add New -> Project,导入 GitHub 仓库。
如果仓库根目录就是 Hexo 项目,配置如下:
1 | Framework Preset: Hexo |
如果项目的 package.json 位于仓库的 blog 子目录,才把 Root Directory 改为 blog。
环境变量通常不需要填写。删除空的示例变量,例如 EXAMPLE_NAME。
package.json 建议包含:
1 | { |
如果没有 build 脚本,可以在项目根目录执行:
1 | npm pkg set scripts.build="hexo generate" |
五、新电脑首次使用
安装 Git 和 Node.js。Node.js 建议与其他电脑及 Vercel 使用相同的主版本,例如 Node.js 20 或 22。
在新电脑执行:
1 | mkdir -p ~/Documents/Hexo |
如果仓库使用了 Git 子模块,应使用:
1 | git clone --recurse-submodules https://github.com/你的用户名/你的仓库.git blog |
已经克隆过但主题目录为空时:
1 | git submodule update --init --recursive |
启动本地预览:
1 | npx hexo clean |
浏览器打开:http://localhost:4000
六、日常工作流程
开始工作前,先同步远程版本:
1 | cd ~/Documents/Hexo/blog |
如果 package.json 或 package-lock.json 有变化,再执行:
1 | npm ci |
修改文章或配置后,本地检查:
1 | npx hexo clean |
确认没有问题后提交:
1 | git status |
推送后,Vercel 会自动创建新的部署。部署状态显示 Ready 后,线上网站就是最新版本。
七、主题和 node_modules
如果主题是通过 npm 安装到 node_modules:
- 不要把
node_modules提交到 GitHub。 - 必须提交
package.json和package-lock.json。 - 新电脑和 Vercel 会通过
npm ci自动重新安装主题。 - 不要直接修改
node_modules中的主题文件,因为这些修改会在npm ci、换电脑或 Vercel 部署时消失。
主题更新应使用:
1 | npm install hexo-theme-主题名@latest --save |
如果只是增加自定义 JS,优先把文件放在:
1 | source/js/custom.js |
再按照主题文档提供的 inject、custom_js 或页脚配置加载。
如果必须修改 npm 主题内部源码,可以使用 patch-package 保存差异;长期大量修改则建议 Fork 主题仓库。不要只在 node_modules 中手动改完就结束。
八、图片、密钥和冲突
文章图片可以放在:
1 | source/images/ |
不要把图片或文件放在 public/,因为 hexo clean 会删除该目录。
不要把密码、API Token 或私钥提交到 GitHub。线上密钥放到 Vercel 的 Environment Variables;本地需要时在本机单独配置,并确保相关文件被 .gitignore 忽略。
不要在两台电脑上同时修改同一篇文章或同一份配置。切换电脑时遵循:
1 | 开始:git pull --rebase |
如果 git pull 产生冲突,先解决冲突、测试本地生成,再提交并推送;不要在未解决冲突时强行推送。
九、常用排查命令
1 | git status |
最简记忆:
1 | 新电脑:git clone -> npm ci |