教程:解决冲突与卡住问题

Hexo 部署 GitHub 教程:解决冲突与卡住问题

在使用 Hexo 部署博客到 GitHub 仓库的过程中,经常会遇到以下问题:

  1. hexo d 卡住,推送失败
  2. 远程分支比本地更新,导致 push 被拒绝
  3. 文件冲突(例如同一篇文章在本地和远程都修改过)

本文将详细讲解这些问题的原因及解决方案。


1. Hexo 部署流程回顾

Hexo 部署 GitHub 的核心步骤:

BASH
1
2
3
hexo clean       # 清理生成的缓存文件
hexo g # 生成静态文件到 public 文件夹
hexo d # 部署到远程仓库

部署时,Hexo 会在根目录生成 .deploy_git 目录,临时存放 Git 仓库内容,然后 push 到远程。


2. 部署卡住 / push 中断

现象

APACHE
1
Writing objects:  34% (1089/3197), 3.09 MiB | 167.00 KiB/s
  • 打包成功,但推送到 GitHub 时卡住不动。

  • 常见原因:

    1. SSH 直连 GitHub 网络不稳定
    2. 一次性提交文件太多
    3. 代理/端口未配置好

解决方法

方法 1:确保 SSH 使用 443 端口

~/.ssh/config 中添加:

SSH
1
2
3
4
Host github.com
HostName ssh.github.com
Port 443
User git

Hexo _config.yml 中保持:

YAML
1
2
3
4
deploy:
type: git
repo: git@github.com:用户名/仓库名.git
branch: main

方法 2:删除 .deploy_git 重新部署

BASH
1
2
rm -rf .deploy_git
hexo clean && hexo g && hexo d

方法 3:改用 HTTPS 部署(更稳定)

YAML
1
2
3
4
deploy:
type: git
repo: https://github.com/用户名/仓库名.git
branch: main

配置 GitHub Token,HTTPS 推送更稳定。


3. 远程分支比本地更新(non-fast-forward)

现象

XL
1
2
! [rejected] befe -> befe (fetch first)
Updates were rejected because the remote contains work that you do not have locally.

解决方法

方法 1:合并远程更新(推荐)

BASH
1
2
3
4
git fetch origin
git merge origin/befe
# 解决冲突后
git push origin befe

方法 2:直接覆盖远程(危险,但适合 Hexo 部署分支)

BASH
1
git push origin befe --force

4. 文件冲突(add/add)

现象

PGSQL
1
2
CONFLICT (add/add): Merge conflict in source/_posts/xxx.md
Automatic merge failed; fix conflicts and then commit the result.

解决步骤

  1. 查看冲突文件
BASH
1
git status
  1. 决定保留版本:
  • 保留本地版本:
BASH
1
git checkout --ours path/to/file
  • 保留远程版本:
BASH
1
git checkout --theirs path/to/file
  • 手动合并两者内容,删除 <<<<<<< 等标记
  1. 标记已解决:
BASH
1
git add path/to/file
  1. 提交:
BASH
1
git commit -m "解决冲突"
  1. 推送:
BASH
1
git push origin befe

💡 如果这是 Hexo 部署分支,通常直接保留本地版本并强制 push 是最简单的方式:

BASH
1
2
3
git add .
git commit -m "覆盖远程内容,解决冲突"
git push origin befe --force

5. 总结建议

  1. Hexo 部署分支建议专用,如 gh-pages,避免和主分支冲突
  2. 多账户 SSH 用 Host 别名管理,User 一律用 git
  3. 网络不稳可用 HTTPS + Token 或 SSH 443 端口
  4. 遇到冲突,手动解决或直接强制覆盖远程

这样一套操作流程,可以保证 Hexo 部署稳定,不会再卡住,也能顺利更新远程仓库。