一、部署架构 本博客采用 双仓库分离架构 :
1 2 3 4 graph TB A[本地 Hexo 源码] -->|git push source| B[GitHub<br/>hexo-blog-source] A -->|hexo g + hexo d| C[GitHub<br/>bedhere.github.io] C --> D[GitHub Pages<br/>https://bedhere.github.io]
源码仓库 :hexo-blog-source - 备份文章、主题、配置
Pages 仓库 :bedhere.github.io - 存放静态页面,自动部署上线
二、Git 部署方式 2.1 HTTPS 方式(初始推荐) 优点 :配置简单,无需生成密钥缺点 :每次推送需输入账号密码
1 2 3 4 5 6 git remote add source https://github.com/bedhere/hexo-blog-source.git git remote add pages https://github.com/bedhere/bedhere.github.io.git git remote -v
2.2 SSH 方式(长期推荐) 优点 :一次配置永久使用,无需重复输入密码缺点 :首次需生成 SSH Key
(1)生成 SSH Key 1 2 ssh-keygen -t rsa -C "your@email.com"
💡 提示:一路回车即可,默认保存在 ~/.ssh/id_rsa.pub
(2)复制公钥 1 2 3 4 5 cat ~/.ssh/id_rsa.pubnotepad ~/.ssh/id_rsa.pub
复制全部内容(以 ssh-rsa 开头)
(3)添加到 GitHub
登录 GitHub → 右上角头像 → Settings
左侧菜单 SSH and GPG keys → New SSH key
粘贴公钥内容 → Add SSH key
(4)切换为 SSH 地址 1 2 3 4 5 6 git remote set-url source git@github.com:bedhere/hexo-blog-source.git git remote set-url pages git@github.com:bedhere/bedhere.github.io.git ssh -T git@github.com
看到 Hi bedhere! You've successfully authenticated 即成功。
三、Hexo 博客部署实战 3.1 初始化 Git 仓库 1 2 3 4 5 6 7 8 9 cd E:\BlogFilegit init git remote add source https://github.com/bedhere/hexo-blog-source.git git remote add pages https://github.com/bedhere/bedhere.github.io.git
3.2 配置 .gitignore 创建 .gitignore 文件,排除无关文件:
1 2 3 4 5 6 7 8 9 10 node_modules/ public/ db.json *.log .DS_Store Thumbs.db .vscode/ .idea/ .deploy*/ themes/butterfly/.git
3.3 提交源码到远程仓库 1 2 3 4 5 6 7 8 git add . git commit -m "init: hexo source backup" git push -u source main
3.4 部署静态页面到 Pages 1 2 hexo clean && hexo g && hexo d
部署原理 :
hexo g:生成 public/ 目录
hexo d:将 public/ 推送到 _config.yml 中配置的 repo 地址
1 2 3 4 5 deploy: type: git repo: https://github.com/bedhere/bedhere.github.io.git branch: main
四、Git 常用指令速查 4.1 基础操作
指令
作用
示例
git init
初始化 Git 仓库
git init
git clone <url>
克隆远程仓库
git clone https://github.com/user/repo.git
git status
查看文件状态
git status
git add <file>
添加文件到暂存区
git add .
git commit -m "msg"
提交到本地仓库
git commit -m "fix bug"
git push <remote> <branch>
推送到远程
git push source main
git pull <remote> <branch>
拉取远程更新
git pull source main
4.2 分支管理
指令
作用
示例
git branch
查看分支
git branch
git branch <name>
创建分支
git branch dev
git checkout <name>
切换分支
git checkout dev
git checkout -b <name>
创建并切换
git checkout -b feature
git merge <name>
合并分支
git merge dev
4.3 远程仓库
指令
作用
示例
git remote -v
查看远程仓库
git remote -v
git remote add <name> <url>
添加远程仓库
git remote add origin https://...
git remote remove <name>
删除远程仓库
git remote remove origin
git remote set-url <name> <url>
修改远程地址
git remote set-url origin git@...
4.4 撤销与回退
指令
作用
示例
git reset --soft HEAD^
撤销上一次 commit(保留更改)
git reset --soft HEAD^
git reset --hard HEAD^
彻底撤销上一次 commit
git reset --hard HEAD^
git checkout -- <file>
丢弃工作区修改
git checkout -- config.yml
git revert <commit-id>
生成反向提交撤销指定 commit
git revert abc123
4.5 日志查看
指令
作用
示例
git log
查看提交历史
git log
git log --oneline
简洁显示
git log --oneline
git log --graph
图形化显示分支
git log --graph
五、Hexo 博客日常工作流 1 2 3 4 5 6 7 8 graph LR A[新建文章] --> B[撰写内容] B --> C[hexo s 预览] C --> D{满意?} D -->|否 | B D -->|是 | E[hexo clean && hexo g] E --> F[git add + commit + push] F --> G[hexo d 部署]
标准流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 hexo new post "文章标题" hexo s hexo clean && hexo g && hexo d git add . git commit -m "feat: 新增文章《文章标题》" git push source main
六、常见问题 6.1 推送失败:Permission denied 错误信息 :
1 2 fatal: Could not read from remote repository. Please make sure you have the correct access rights
解决方案 :改用 SSH 方式(见 2.2 节)
6.2 部署后页面未更新 排查步骤 :
确认 hexo g 成功生成 public/
确认 hexo d 推送到正确的仓库
清除浏览器缓存(Ctrl + Shift + Delete)
检查 GitHub Pages 设置:Settings → Pages → Source 是否为 main 分支
6.3 误将 public/提交到源码仓库 1 2 3 4 5 6 7 git filter-branch --force --index-filter \ "git rm -rf --cached --ignore-unmatch public" \ --prune-empty --tag-name-filter cat -- --all git push source --force --all
6.4 多人协作冲突 1 2 3 4 5 6 7 8 9 git pull source main git add . git commit -m "merge: 解决冲突" git push source main
七、总结 核心要点 :
✅ 双仓库分离 :源码与部署产物分开管理
✅ SSH 优先 :一次配置永久使用,避免反复输入密码
✅ 定期备份 :每次写文章后记得 git push source main
✅ 部署顺序 :先 hexo g 生成,再 hexo d 部署
最佳实践 :
1 2 3 4 5 6 hexo new post "标题" hexo s hexo clean && hexo g hexo d git add . && git commit -m "msg" && git push source main