起因

本博客使用 Butterfly 主题,最近 Butterfly 从 3.8.4 更新到了 4.0.1。要更新主题,有电脑的时候,可以直接用 Github Desktop+Git Bash,然而我身边只有手机,没法使用 Github Desktop 更新 Github 上的博客源码。所以想着用手机更新博客。

  1. 开端
  2. 发展
  3. 高潮
  4. 结尾

开端

工欲善其事必先利其器,首先下载安卓模拟终端 Termux

更换为清华源

1
2
3
4
5
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list

pkg update

安装包

1
pkg install vim curl wget git unzip unrar nodejs-lts openssh -y && npm install hexo-cli -g

发展

配置 Personal access tokens

如果你的博客是公共仓库,你可以跳过这段,如果是私有仓库,则必须配置 Personal access tokens。下面是配置 Github Personal access tokens 的过程。

  1. 登陆 Github。
  2. 转到 Settings/Developer settings/Personal access tokens。
  3. 点 Generate new token。
  4. 设置有效时间,Scopes 全选上。
  5. 最后点 Generate token,复制保存好 token,待会验证 Github 账号需要它。

克隆仓库

1
git clone https://github.com/用户名/仓库名.git

如果仓库私有,接着输入用户名,在 Password 一行填刚刚的 token。

1
2
cd 仓库名
npm install

检查

1
hexo clean && hexo g && hexo s

如果不出意外的话,浏览器打开 http://localhost:4000 ,就能看到博客内容了。

高潮

首次准备

为当前博客目录配置用户和邮箱:

1
2
git config user.name 用户名
git config user.email 邮箱

添加远程地址:

1
git remote add origin git@github.com:用户名/仓库名.git

主要过程

克隆完仓库,对它操作后,用 git status 查看工作区与暂存区的差异(项目的改变)。

接着将改变从工作区添加到暂存区。

git add . 直接添加全部。

接着将暂存区的文件提交至本地库并添加注释。

1
git commit -m "手机更新"

最后,将本地库的项目推至远程库。

1
git push origin 分支名 # 分支名可以是 master/main 等

一些报错

  1. 可能不存在 push 的分支。
1
2
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/用户名/仓库名.git'
  1. 有较新的提交,需要先 pull
1
2
 ! [rejected]        分支名 -> 分支名 (fetch first)
error: failed to push some refs to 'github.com:用户名/仓库名.git'

解决方法:

1
2
git pull --rebase origin 分支名
git push origin 分支名

结尾

从本地到云端:

1
2
git status
git add . && git commit -m "手机更新" && git push origin 分支名

从云端到本地:

1
git fetch origin 分支名 && git merge origin/分支名

如果不确定哪边的内容最新用 git log origin/分支名 查看最近提交记录。