hint: 进行以下操作前需要在GitHub和gitee上创建仓库并用Git配置两个账户的SSH公钥

存放网站的仓库必须设置成公开的仓库. 存放网站源码的仓库随便

SSH公钥的配置可以参考:

以下命令需要在网站根目录下打开的终端(或者cd到根目录下)执行

安装依赖的插件

1
npm install

该命令会自动安装package-lock.json里面中列出的依赖包。(当中包含将网站上传到GitHub要用到的hexo-deployer-git 插件)

网站上传到GitHub仓库

修改网站根目录下的 _config.yml,配置 GitHub相关信息

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

上传网页

1
hexo cl&&hexo g -d

本地预览网页(和上传没关系,就是说一下)

1
hexo cl&&hexo s

上面的两行命令可以分开执行,写在一行方便且快捷

网站页源码上传到gitee仓库

初始化本地仓库(第一次运行需要)

1
git init

设置用户名和邮箱(第一次运行需要)

每次上传都要填写提交信息(例如, 修改了xxx文件), 提交信息上会记录你的用户名和邮箱

1
git config user.name 'yourname'
1
git config user.email 'youremail'

设置远程仓库的地址(第一次运行需要)

remotename可以自己起一个名字, 方便记忆(例如, 这个远程仓库的名字我可以起gitee-hexo)

remotepath是远程仓库的地址, 可以点击仓库的右上角的按钮查看(例如, 我的远程地址是https://gitee.com/o0w0b/hexo-source.git)

1
git remote add remotename remotepath

添加本地文件到暂存区

1
git add .

提交到本地工作区

1
git commit -m "在这里写本次的提交信息"

上传到远程仓库

master这个是远程仓库的分支的名字(在gitee创建仓库后, 自动创建的主分支的名字)

1
git push remotename master

修改在本地记录的远程仓库信息的方法

运行git init之后, 会发现根目录下多了名字为.git的隐藏文件夹

进入该文件夹, 用编辑器打开config文件

我的config文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = o0w0b
email = 2362760448@qq.com
[remote "gitee-hexo"]
url = https://gitee.com/o0w0b/hexo-source.git
fetch = +refs/heads/*:refs/remotes/gitee-hexo/*

然后修改对应的内容就可以

其中[remote]下的fetch部分表示将远程仓库(gitee-hexo)的所有分支(refs/heads/*)与本地的远程跟踪分支(refs/remotes/gitee-hexo/*)进行关联

url是要从本地推送到远程仓库的地址, url可以有多个(可以通过设置远程仓库的地址添加)

1
2
3
4
5
6
[remote "gitee-hexo"]
url = https://gitee.com/o0w0b/hexo-source.git
fetch = +refs/heads/*:refs/remotes/gitee-hexo/*
[remote "github-hexo"]
url = https://github.com/o0w0b/hexo-source.git
fetch = +refs/heads/*:refs/remotes/github-hexo/*

通过分别执行

1
2
git push gitee-hexo master
git push github-hexo main

推送到每个远程仓库