通过git运用在github仓库中备份hexo博客

每次新增一篇博文后备份到 github 仓库中,这样是比较合理的。今天也算是初步学会了怎样去操作。

逻辑上我们应该在搭建博客之初的第一步创建一个空的source仓库,然后 clone 到本地,然后,在这个所 clone 的文件夹下搭建博客。其中 clone 的操作命令如下:

1
2
git clone https://github.com/username/仓库名.git  
// 替换成自己的仓库

下面记录下 git 的用法。

如何上传

若 git 版本太低,链接不上远程时,需要先更新下 git。

至于如何上传,主要参考的这篇博文——使用git将项目上传到github(最简单方法),还有这篇博文。主要的步骤是:

1
2
3
4
5
6
1. git init //初始化仓库
2. git add .(文件name) //添加文件到本地仓库
3. git commit -m "first commit" //添加文件描述信息
4. git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支;
// 若失败,git remote rm origin 后重复。
5. git push -u origin master //把本地仓库的文件推送到远程仓库

有可能出现错误:

1
2
3
4
5
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

根据提示在步骤 4 和 5 之间添加一行:

1
2
3
4
5
6
1. git init //初始化仓库
2. git add .(文件name) //添加文件到本地仓库
3. git commit -m "first commit" //添加文件描述信息
4. git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支;
5. git pull origin master // 把本地仓库的变化连接到远程仓库主分支
6. git push -u origin master //把本地仓库的文件推送到远程仓库

如果远程分支我们不需要了,我们可以运用命令强制覆盖掉

1
git push origin master -f

.gitignore 的写法

主要参考着两篇博文,.gitignore详解Git忽略提交规则 - .gitignore配置运维总结过

用到的几个:

1
2
3
 /mtk/           表示过滤整个文件夹
*.zip 表示过滤所有.zip文件
/mtk/do.c 表示过滤某个具体文件

以及:

1
2
3
4
5
6
7
# 此为注释 – 将被 Git 忽略

*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

可以通过一些过滤方法,过滤掉一些没有必要备份的文件。

另外

如果链接远程仓库git remote add origin xxx.git时出现如下的错误信息:

1
fatal: remote origin already exists.

可以这样解决

1
2
3
1、先输入$ git remote rm origin

2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!

然后再使用之前的命令应该就可以链接上去了。

其实这个链接已经存在,不再链接应该也是可以的,直接 push 就行了。

博客从头到尾的搭建可以参考这篇非常详细的博文

感谢支持!