《Git分布式版本控制详解手册》

关于作者

周李杰,最后一批90后,现居湖北武汉

孤独患者,懒癌中期,无脑幻想者。但却人畜无害,无需远离。

兴趣众多,然无一精通。正努力成为一个有趣的人。

1
2
> print("👆以上是不完整的我") //请我吃大餐将会了解更多
>

[TOC]

开篇

学会使用Markdown进行编写文本文件
作为一名新时代的IT知识分子,熟悉Markdown语法至关重要
Markdown是一种轻量级标记语言,由于Markdown的轻量化、易读易写等特点
并且Markdown对于图片,图表、数学式都有支持
当前许多网站都在广泛地使用Markdown来撰写帮助文档或是用于论坛上发表消息
甚至Markdown都已经被用来撰写电子书
初学者可以使用Typora来写Markdown,进而提高自己的写作能力,熟悉语法之后,可尝试使用vim或者Visual Studio Code来进行编写

这里附带一下Typora的下载链接,点击此处打开,选择下载

本篇手册就基于Markdown语法编写

Git版本控制

1-Git版本控制的来历

全称:Git分布式版本控制系统
在IT行业中,Linux系统无人不知无人不晓,在1991年linus创建了Linux并将它开源,早期的Linux就仅仅只是一个框架,就连我们熟知的vim也是后来开发者们不断开发出来的。

在这里简要提示一下:vim最初是vi,vim第一个版本是由一个叫布莱姆荷兰人在1991年开发

由于Linux系统是开源,并吸引了世界各地的开发者为Linux提供代码服务,因此Linux系统不断的发展壮大,现绝大部分互联网公司服务器运用的都是Linux系统

那么,linus是怎么管理这些代码呢,如何进行区分以及修改呢?

早在2002年以前,世界各地的开发者们通过diff的方式将源代码发送给linus,然后由linus手工合并。

到了2002年,Linux经过了10年的历程,其中的源代码早已经非常庞大了,代码库之大让linus很难再使用手工的方式管理了,于是呢,linus脑袋一转,选择了一个商业性的版本控制系统BitKeeper,这个控制系统的老东家BitMover公司处于人道主义,授权给Linux社区免费使用这个版本控制系统。

不过好景不长,时隔三年,也就是在2005年的时候,一位叫Andrew(著名的samba服务由他开发而来)试图破解BitKeeper的协议,不料BitMover公司正是搞监控的,后来大家也应该能猜到,BitMover公司收回了Linux社区使用BitKeeper的版权。

Linus完全可以出面和BitMover公司交谈一下,并管好自己社区的弟兄,但是linus没有,其实linus内心早就想写一个分布式控制系统来管理自己的代码仓库。

Linus花了两周的时间,运用C语言写了一个分布式控制系统,Git由此而诞生,linus在一个月内,就将linux系统中的源码交接到Git中管理。

Git迅速就成为了最流行的分布式版本控制系统,尤其是在2008年,GitHub网站(被国内称之为程序员社交网站)上线,它为开源项目免费提供Git存储,无数开源项目开始迁移至GitHub,包括jQuery,PHP,Ruby等。

2-安装Git

此章节主要所讲在Linux以及windows系统上怎么安装Git分布式版本控制

Git是在linux上进行开发的,所以理所应当的用到Linux和unix上跑,不过,慢慢的有些大牛就将Git移植到windows上跑,现在Git不仅可以在Linux、unix、windows上跑,还来可以在mac系统上运行。

在Linux上安装Git

这里就以RHEL 7版本举例

第一步:首先检查系统是否安装Git

1
2
[root@client111 ~]# git
-bash: /usr/bin/git: 没有那个文件或目录

若您有信息,则跳过第二步骤,这个信息类似一个帮助文档

第二步:安装Git

首先清空本地所有yum源,并使用网易yum源

1
2
3
4
5
6
7
8
[root@client111 ~]# cd /etc/yum.repos.d/
[root@client111 yum.repos.d]# rm -rf *
[root@client111 yum.repos.d]# ls
[root@client111 yum.repos.d]# wget https://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@client111 yum.repos.d]# ls
CentOS7-Base-163.repo
[root@client111 yum.repos.d]# yum clean all
[root@client111 yum.repos.d]# yum makecache

查询是否有Git软件包

1
2
3
4
5
6
7
8
9
[root@client111 yum.repos.d]# cd
[root@client111 ~]# yum info yum
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
已安装的软件包
名称 :yum
架构 :noarch
版本 :3.4.3
···········

安装Git

1
2
3
4
5
6
7
[root@client111 ~]# yum install git -y
············
已安装:
git.x86_64 0:1.8.3.1-20.el7
作为依赖被安装:
perl-Git.noarch 0:1.8.3.1-20.el7
完毕!

查看是否安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@client111 ~]# git
··········
最常用的 git 命令有:
add 添加文件内容至索引
bisect 通过二分查找定位引入 bug 的变更
branch 列出、创建或删除分支
checkout 检出一个分支或路径到工作区
clone 克隆一个版本库到一个新目录
commit 记录变更到版本库
diff 显示提交之间、提交和工作区之间等的差异
fetch 从另外一个版本库下载对象和引用
grep 输出和模式匹配的行
init 创建一个空的 Git 版本库或重新初始化一个已存在的版本库
log 显示提交日志
merge 合并两个或更多开发历史
mv 移动或重命名一个文件、目录或符号链接
pull 获取并合并另外的版本库或一个本地分支
push 更新远程引用和相关的对象
rebase 本地提交转移至更新后的上游分支中
reset 重置当前HEAD到指定状态
rm 从工作区和索引中删除文件
show 显示各种类型的对象
status 显示工作区状态
tag 创建、列出、删除或校验一个GPG签名的 tag 对象

命令 'git help -a''git help -g' 显示可用的子命令和一些指南。参见
'git help <命令>''git help <指南>' 来查看给定的子命令帮助或指南。

注意事项

  • 如果您的系统是ubuntu或者Debian系统的话,请使用sudo get install git -y命令进行安装
  • 如果还是不行,请使用sudo apt-get install git -y命令进行安装
  • 最后还是不行,请移步至官网下载源码,用tar解压,然后编译安装,编译安装步骤:./configmakesudo make install

    如果你所装的系统比较先进,基本二三条是不用看的

在windows上安装Git

第一步:移步官网下载最新版本

点击此处访问Git官网下载界面
现在最新版本是2.23.0,于2019年8月16日发布

下载成功后请使用傻瓜式安装方法,安装地址自行输入,建议在C盘.

使用Git

安装完成之后,在桌面单机鼠标右键会多出两个选项Git GUI HereGit Bash Here.

选择Git Bash Here点击,此时会蹦出一个界面

输入git,如果报出以下帮助文档便是安装正确

1
2
3
4
5
6
7
8
9
10
$ git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
···············
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

以上就是介绍怎么安装Git,此详解手册仅仅介绍linu以及windows,其他系统安装方法也大致相同,具体可进Git官方网站阅读

3-创建账户

绑定用户名

绑定用户在上传文件的时候就知道是谁上传的,起到一个表示作用

1
2
3
4
5
6
[root@client111 ~]# git config --global user.name "Jack"
[root@client111 ~]# git config --global user.email "18371207979@139.com"
[root@client111 ~]# git config user.name
Jack
[root@client111 ~]# git config user.email
18371207979@139.com

创建一个目录

创建好一个目录,这个目录将作为一个版本仓库
[root@client111 ~]# cd
[root@client111 ~]# mkdir git-test
[root@client111 ~]# cd git-test/

使用Git管理此仓库
[root@client111 git-test]# git init
初始化空的 Git 版本库于 /root/git-test/.git/

4-连接进Github网站

创建Github账号

点击进入Github官方网站

Github网站挺有名,全球最大的源代码托管服务平台,2018年6月4日,微软宣布以75亿美元的股票收购GitHub。在21世纪互联网时代,我们IT人员应该理所应当地知道这个网站。

进入网站之后点击右上角注册账户,也可以直接点击此处开始注册,注册之中,遵循能跳过则跳过,不能跳过则认真填的原则。

进入邮箱验证界面之后,无论界面怎么样,请点击左上角一个狸猫的图片,然后点击start a project,进入创建一个新的仓库界面。或者验证完成之后直接点击此处看能否跳转创建新的仓库界面

Repository name 这是存储库的名称,随便命名就行,这里我命名为chinesejack
Description 存储库的说明,可选输入,可输入,也可不输入
Public 请选择此选项,任何人都可以看到此存储库。您选择谁可以提交
Initialize this repository with a README
使用自述文件初始化此存储库,勾选则生成README.md一个解释文件,也可以不用勾选,根据自生所需,建议勾选

最后直接鼠标单击create repository创建仓库

ssh密钥链接进Github网站

生成本机密钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 默认直接回车即可
[root@client111 git-test]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bFQH+qf7m9/ruwsE2P5Ho3x0wzHbMN8YY+J24DXTo+M root@client111
The key's randomart image is:
+---[RSA 2048]----+
| o.. |
| oo. . |
| o. oo X+.|
| o ..o.=oXB|
| S ..=+oB=|
| . +=.= +|
| . E o |
| . .+. |
| ..+oo**|
+----[SHA256]-----+
[root@client111 git-test]# cd ~/.ssh/
[root@client111 .ssh]# ls
id_rsa id_rsa.pub
[root@client111 .ssh]# cat id_rsa.pub
····································

将生成出来的密钥复制
进入Github网站,登陆自己刚注册的账号,右上角自己的头像旁边的扩展箭头点一下

然后再点击Settings选项,然后再单击SSH and GPG keys选项

选择第一个New SSH key新建SSH keys密钥

Title随意填写,这里我填写root@client111

Key密钥将刚复制的密钥粘贴进去

此时此刻便连接进你的GitHub仓库

5- Git常用参数

clone-克隆

clone
标准意为:克隆一个仓库到一个新目录
可以理解为:将远端的仓库克隆到本地
命令:git clone [链接]
举例:

1
2
3
4
5
6
7
8
[root@client111 git-test]# git clone https://github.com/chineseJack/chinesejack
正克隆到 'chinesejack'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@client111 git-test]# ls
chinesejack

add-添加

add
标准意为:添加文件内容至索引
可以理解为:提交到暂缓区
命令:git add [文件]
举例:

1
2
3
4
5
6
7
8
9
10
# 单个文件提交到暂存
[root@client111 git-test]# cd chinesejack/
[root@client111 chinesejack]# touch 123.txt
[root@client111 chinesejack]# git add 123.txt
# 多个文件提交到暂存区
[root@client111 chinesejack]# touch abc.txt 456.txt def.txt
[root@client111 chinesejack]# ls
123.txt 456.txt abc.txt def.txt README.md
[root@client111 chinesejack]# git add --all .
# .是代表当前目录

commit-提交

commit
标准意为:记录变更到仓库
可以理解为:将暂存区所有内容提交到当前分支
命令:git commit -m “填写所要描述的内容”
举例:

1
2
3
4
5
6
7
[root@client111 chinesejack]# git commit -m "创建了4个txt文件"
[master 38911e7] 创建了4个txt文件
4 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 123.txt
create mode 100644 456.txt
create mode 100644 abc.txt
create mode 100644 def.txt

push-推

push
标准意为:更新远程引用和相关的对象
可以理解为:推,更新本地到远程仓库
命令:git push

第一次推上去需要后面跟上项目链接,以后就可以不用输入链接
举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@client111 chinesejack]# git push https://github.com/chineseJack/chinesejack
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
'simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Username for 'https://github.com': chineseJack # 此处需要当时注册账户时候的账户名
Password for 'https://chineseJack@github.com': # 此处需要填写当时注册账户时候的密码
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 312 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/chineseJack/chinesejack
50fe4cf..38911e7 master -> master

pull-拉

pull
标准意为:获取并整合另外的仓库或一个本地分支
可以理解为:拉,拉取远程仓库到本地
命令:git pull
举例:

1
2
# 手法如同push
git pull https://github.com/chineseJack/chinesejack

show-查看内容

show
标准意为:显示各种类型的对象
可以理解为:检查你所提交的版本的减少以及增加的内容
命令:git show
举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@client111 chinesejack]# git show
commit 38911e7c2226a033779fdcf7b2eef9c19cb477bf
Author: Jack <18371207979@139.com>
Date: Tue Oct 29 20:58:34 2019 +0800

创建了4个txt文件

diff --git a/123.txt b/123.txt
new file mode 100644
index 0000000..e69de29
diff --git a/456.txt b/456.txt
new file mode 100644
index 0000000..e69de29
diff --git a/abc.txt b/abc.txt
new file mode 100644
index 0000000..e69de29
diff --git a/def.txt b/def.txt
new file mode 100644
index 0000000..e69de29

因为我四个文件里什么都没有,所以未显示内容

status-状态

status
标准意为:显示工作区状态
可以理解为:显示状态,提醒你有没有没提交的
命令:git status
举例:

1
2
3
[root@client111 chinesejack]# git status
# 位于分支 master
无文件要提交,干净的工作区

这里改动一个文件,然后再看下状态

1
2
3
4
5
6
7
8
9
10
[root@client111 chinesejack]# echo 123456789 > 123.txt
[root@client111 chinesejack]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: 123.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a"

如果git status告诉你有文件被修改过,用git diff可以查看修改内容

diff-对比

diff
标准意为:显示提交之间、提交和工作区之间等的差异
可以理解为:你写的对比提交了的有哪些变化
命令:git diff

后面还可以接版本号,与各个版本之间对比
举例:

1
2
3
4
5
6
7
[root@client111 chinesejack]# git diff
diff --git a/123.txt b/123.txt
index e69de29..28d1445 100644
--- a/123.txt
+++ b/123.txt
@@ -0,0 +1 @@
+123456789

log-日志

log
标准意为:显示提交日志
可以理解为:查看提交历史
命令:git log
举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@client111 chinesejack]# git log
commit fd6c7f54c502a2a575e4450429892ec4d1dc3e48
Author: Jack <18371207979@139.com>
Date: Tue Oct 29 21:12:21 2019 +0800

不小心将全部文件删除

commit 38911e7c2226a033779fdcf7b2eef9c19cb477bf
Author: Jack <18371207979@139.com>
Date: Tue Oct 29 20:58:34 2019 +0800

创建了4个txt文件

commit 50fe4cf75396b3e25d9ca51aa21c8a751f52ea6d
Author: chineseJack <57097857+chineseJack@users.noreply.github.com>
Date: Mon Oct 28 20:51:58 2019 +0800

Initial commit

# commit:意为版本

reset –hard-版本退回

reset –hard
标准意为:倒版本号
可以理解为:后面接版本编号,直接回到那个版本
命令:git reset –hard [版本号]
举例:

1
2
3
4
[root@client111 chinesejack]# git reset --hard 38911e7
HEAD 现在位于 38911e7 创建了4个txt文件
[root@client111 chinesejack]# ls
123.txt 456.txt abc.txt def.txt README.md

reflog-历史提交版本

reflog
标准意为:查看命令历史
可以理解为:查看提交历史
命令:git reflog
举例:

1
2
3
4
5
[root@client111 chinesejack]# git reflog
38911e7 HEAD@{0}: reset: moving to 38911e7
fd6c7f5 HEAD@{1}: commit: 不小心将全部文件删除
38911e7 HEAD@{2}: commit: 创建了4个txt文件
50fe4cf HEAD@{3}: clone: from https://github.com/chineseJack/chinesejack

然后再使用reset –hard可回到’未来’版本

1
2
3
4
[root@client111 chinesejack]# git reset --hard fd6c7f5
HEAD 现在位于 fd6c7f5 不小心将全部文件删除
[root@client111 chinesejack]# ls
[root@client111 chinesejack]#

6- Git部分参数

git fetch
从服务器下载数据库,并放到新分支,不跟自己的数据库合并

git whatchanged
查看两个分支的变化

git branch
创建分支,查看分支,删除分支

git checkout
切换分支

git merge
合并分支,把目标分支合并到当前分支

git config
配置相关信息,例如email 和name

git tag
标定版本号

git mv
重命名文件或者文件夹

git rm
删除文件或者文件夹

git help
查看帮助,还有几个无关紧要的命令,请自行查看帮助

7-git版本控制重点知识梳理

本地(local)

初始化
1
2
3
4
5
6
7
8
9
10
全局变量:
git config -global user name "chinesejack"
git config -global email "18371207979@139.com"

初始化新版本库:git init

添加新文件到版本库:
添加单个文件:git add somefile.txt
添加所有txt文件:git add *.txt
添加所有文件:git add
日常操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
提交:
提交所有修改文件:git commit -m "some msg" -a
提交单个文件:git commit -m "add msg to readme.txt" readme.txt
提交增补:git commit -C head -a -amend (此提交增补不会产生新的提交历史记录)

撤销修改:
撤销尚未提交的修改:
撤销1、2个文件:git checkout head readme.txt todo.txt
撤销所有txt文件:git checkout head *.txt
撤销所有文件:git checkout head
撤销提交:
复位:
取消暂存:git reset head或者git reset head <filename>
复位到head之前那个版本:git reset --hard head^

分支:
列出本地分支:git branch
列出所有分支:git branch -a
基于当前分支的末梢创建新的分支:git branch <branchname>
检出分支:git checkout <branchname>
基于当前分支的末梢创建新分支并检出分支:git checkout <branchname>
基于某次提交、分支或标签创建新分支:git branch emputy2 emputy
git branch emputy bfe57de0 用来查看某个历史断面很方便
合并分支:
普通合并:
合并提交:git merge <branchname>
合并但不提交:git merge --no-commit
压合合并:
压合合并后直接提交:git merge --squash <branchname>
压合合并但不提交:git merge --squash --no-commit <branchname>
拣选合并:挑选某次提交合并但不提交:git cherry-pick --no-commit 5b62b6

重命名分支:
git branch -m <branchname> <newname> 不会覆盖已存在的同名分支
git branch -M <branchname> <newname> 会覆盖已存在的同名分支

删除分支:
git branch -d new2 如果分支没有被合并会删除失败
git branch -D new2 即使分支没有被合并也照删不误

标签:
创建标签:
git tag 1.0 为当前分支最近一次提交创建标签
git tag contacts_1.1 contacts 为contacts分支最近一次提交创建标签
git tag 1.1 4e6861d5 为某次历史提交创建标签
显示标签列表:git tag
检出标签:git checkout 1.0
由标签创建分支:git branch b1.1 1.1/git chech=kout -b b1.0 1.0
删除标签:git tag -d 1.0

查看状态:
当前状态:git status
历史记录:git log
当前分支历史记录:gitk
查看某分支历史记录:gitk <branchname>
查看所有分支:gitk branch -v
每个分支最后的提交:git branch -v

导出版本库:
git archive --format=zip head>nb.zip
git archive --format-zip --prefix=nb1.0/ head>nb.zip

远程(remote)

初始化
1
2
3
4
5
6
7
克隆版本:git clone <url>

别名:
添加远程版本库的别名:git remote add <别名> <远程版本库的url>
删除远程库的别名和相关分支:git remote rm <别名>

创建一个无本地分支的库:git init -bare
日常操作
1
2
3
4
5
6
7
8
9
分支:
列出远程分支:git branch -r
删除远程库中已经不存在的分支:git remote prune origin

从远程库获取:
获取但不提交:git fetch <远程版本库>
获取并提交到当前本地分支:git pull

推入远程:git push origin master

生成SSH Key

1
2
3
第一步:启动git Bash控制台
第二步:生成SSH Key:ssh-keygen -t rsa -C "18371207979@139.com"
第三步:将SSH Key添加到Github账户里

8-使用Github+Hexo搭建个人博客

效果可点击此处

当您看到这里,我就默认你掌握了git的全部常用知识。如果还不是不懂,请返回到手册的开头,再看一遍。

创建个人仓库

  • 第一步 ssh进自己的GitHub账户
    在前面第四章节的时候已经将本地ssh进github账户了,所以此处不再讲解。

  • 第二步 主界面点击Start a project
    仓库命名请以.github.io结尾
    Description 为可选择填写
    勾选Public
    不勾选Initialize this repository with a README
    点击Create repository

例如本文的仓库名:chineseJack.github.io
Descriptiont这里填写blog

之后就会跳转到创建好仓库,细心的查看,你的仓库会形成一个地址,同时它也可以切换ssh形式或者https形式,这里我们复制ssh形式的链接
例如本手册的创建出来的链接:git@github.com:chineseJack/chineseJack.github.io.git

在做下面一些步骤时,如果你是服务器,那么还好,如果你是在虚拟机上操作,那么建议将处理器的核数调到2核以上,最好4核,不然下面会渴望到放弃。

下载安装Node.js

点击此处下载
下载Linux Binaries (x64)包,将包导入到root的家目录

或者用wget下载

1
2
[root@client111 ~]# wget https://npm.taobao.org/mirrors/node/v10.16.0/node-v10.16.0-linux-x64.tar.xz
[root@client111 ~]# mv node-v10.16.0-linux-x64.tar.xz /usr/local/
  • 安装node.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@client111 ~]# cd /usr/local/
    [root@client111 local]# tar xf node-v10.16.0-linux-x64.tar.xz
    [root@client111 local]# ln -s node-v10.16.0-linux-x64 nodejs
    [root@client111 local]# cd nodejs/
    [root@client111 nodejs]# ls
    bin CHANGELOG.md include lib LICENSE README.md share
    [root@client111 nodejs]# cd bin/
    [root@client111 bin]# ./node -v
    v10.16.0
  • 创建软链接

    1
    2
    3
    4
    5
    6
    [root@client111 bin]# ls
    node npm npx
    [root@client111 bin]# pwd
    /usr/local/nodejs/bin
    [root@client111 bin]# ln -s /usr/local/nodejs/bin/node /usr/local/bin/node
    [root@client111 bin]# ln -s /usr/local/nodejs/bin/npm /usr/local/bin/npm

安装部署Hexo

1
2
3
4
5
6
7
[root@client111 ~]# npm install hexo-cli -g
/usr/local/node-v10.16.0-linux-x64/bin/hexo -> /usr/local/node-v10.16.0-linux-x6 4/lib/node_modules/hexo-cli/bin/hexo
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.1 (node_modules/hex o-cli/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@ 2.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"} )

+ hexo-cli@3.1.0
added 66 packages from 313 contributors in 85.809s

出现报错,不要慌张,依次往下
其实fsevents@2.1.1 为mac系统中的,linux系统可以忽略

  • 创建软链接

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@client111 ~]# cd /usr/local/nodejs/lib/node_modules/hexo-cli/bin/
    [root@client111 bin]# pwd
    /usr/local/nodejs/lib/node_modules/hexo-cli/bin
    [root@client111 bin]# ln -s /usr/local/nodejs/lib/node_modules/hexo-cli/bin/hexo /usr/local/bin/hexo
    [root@client111 bin]# cd
    [root@client111 ~]# vim .bash_profile
    # 将第十行改动如下
    PATH=$PATH:$HOME/bin:/usr/local/bin
    # 保存并退出
    [root@client111 ~]# source /etc/profile
    [root@client111 ~]# hexo
    Usage: hexo <command>

    Commands:
    ·······
    # 如果没有报错则表示hexo安装成功
  • 部署Hexo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    [root@client111 ~]# cd
    [root@client111 ~]# mkdir hexo
    [root@client111 ~]# cd hexo/
    [root@client111 blog]# hexo init
    INFO Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
    正克隆到 '/root/hexo'...
    remote: Enumerating objects: 22, done.
    remote: Counting objects: 100% (22/22), done.
    remote: Compressing objects: 100% (17/17), done.
    remote: Total 153 (delta 8), reused 8 (delta 3), pack-reused 131
    接收对象中: 100% (153/153), 29.67 KiB | 5.00 KiB/s, done.
    处理 delta 中: 100% (70/70), done.
    子模组 'themes/landscape' (https://github.com/hexojs/hexo-theme-landscape.git) 已为路径 'themes/landscape' 注册
    正克隆到 'themes/landscape'...
    remote: Enumerating objects: 32, done.
    remote: Counting objects: 100% (32/32), done.
    remote: Compressing objects: 100% (25/25), done.
    remote: Total 1054 (delta 20), reused 10 (delta 7), pack-reused 1022
    接收对象中: 100% (1054/1054), 3.21 MiB | 3.00 KiB/s, done.
    处理 delta 中: 100% (578/578), done.
    子模组路径 'themes/landscape':检出 '73a23c51f8487cfcd7c6deec96ccc7543960d350'
    INFO Install dependencies
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/nunjucks/node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.1 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

    added 362 packages from 470 contributors and audited 2622 packages in 211.34s
    found 0 vulnerabilities

    INFO Start blogging with Hexo!

这里的警告依然如此不用理会,最后一句Start blogging with Hexo!我们就可以开始用Hexo博客!

生成测试页面

1
2
3
4
[root@client111 hexo]# hexo g
[root@client111 hexo]# hexo s
INFO Start processing
INFO Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.

此时先不用Ctrl+C,先访问http://localhost:4000看看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@client111 ~]# netstat -lntup | grep 'hexo'
tcp6 0 0 :::4000 :::* LISTEN 53413/hexo
[root@client111 ~]# yum install elinks.x86_64 -y
[root@client111 ~]# elinks http://localhost:4000
Link: [1]Hexo (alternate)

[2]Hexo

Home [3]Archives
[4]_____________________ [5]
[6]2019-11-04

Hello World

Welcome to [7]Hexo! This is your very first post. Check [8]documentation
for more info. If you get any problems when using Hexo, you can find the
answer in [9]troubleshooting or you can ask me on [10]GitHub.

Quick Start

Create a new post

1 $ hexo new "My New Post"

More info: [11]Writing

Run server

1 $ hexo server

More info: [12]Server

Generate static files

如果能看到这些信息就表示成功,此时就可以Press Ctrl+C to stop。

生成博客页面

  • 修改配置文件
    1
    2
    3
    4
    5
    6
    7
    8
    [root@client111 hexo]# ls
    _config.yml node_modules package-lock.json scaffolds themes
    db.json package.json public source
    [root@client111 hexo]# vim _config.yml
    # 在最后一行添加:
    type: git
    repo: git@github.com:chineseJack/chineseJack.github.io.git
    branch: master

repo这里请使用上面创建仓库时所复制的链接

  • 生成页面并部署
    1
    2
    3
    4
    [root@client111 hexo]# hexo c && hexo g && hexo d
    ········
    INFO 28 files generated in 410 ms
    ERROR Deployer not found: git

出现报错,不要惊慌
解决办法:

1
2
3
4
5
6
7
8
9
[root@client111 hexo]# npm install --save hexo-deployer-git
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.1 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/nunjucks/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ hexo-deployer-git@2.0.0
added 1 package from 1 contributor and audited 2697 packages in 5.796s
found 0 vulnerabilities

忽视前4行的报错

再进行一次部署

1
2
3
4
5
6
7
8
9
[root@client111 hexo]# hexo c && hexo g && hexo d
············
Username for 'https://github.com': chineseJack # 这里树妖验证你的github账户
Password for 'https://chineseJack@github.com': # 这里需要验证你的GitHub账户密码
·········
To git@github.com:chineseJack/chineseJack.github.io.git
* [new branch] HEAD -> master
分支 master 设置为跟踪来自 git@github.com:chineseJack/chineseJack.github.io.git 的远程分支 master。
INFO Deploy done: git

总结

  • 至此,搭建个人博客已结束,浏览器访问仓库名称就是属于自己的个人博客
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    [root@client111 hexo]# elinks chineseJack.github.io
    Link: [1]Hexo (alternate)

    [2]Hexo

    Home [3]Archives
    [4]_____________________ [5]
    [6]2019-11-04

    Hello World

    Welcome to [7]Hexo! This is your very first post. Check [8]documentation
    for more info. If you get any problems when using Hexo, you can find the
    answer in [9]troubleshooting or you can ask me on [10]GitHub.

    Quick Start

    Create a new post

    1 $ hexo new "My New Post"

    More info: [11]Writing

    Run server
打赏
  • © 2019-2020 Li Jie
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

喜欢就打赏吧~

支付宝
微信