--- layout: post title: "使用 Git 管理静态网站" description: "本博使用 GitHub Pages,很喜欢使用 Git 这种方式管理网站,下面就一起来看看如何使用 Git 来管理静态网站" thumb: IMG_PATH/git.svg category: tech tags: [Git, SSH, VPS] --- * toc {:toc} 本博使用 GitHub Pages,很喜欢使用 Git 这种方式管理网站,下面就一起来看看如何使用 Git 来管理静态网站 以下参考这篇文章 [Using Git to manage a web site](http://toroid.org/ams/git-website-howto) 假设有一台 VPS,它的域名是 fooleap.org,我们可以通过 SSH 登录进行操作 先在服务器上创建一个新账户 git $ ssh root@fooleap.org 'adduser git' ## SSH 自动登录 使用公钥互信实现自动登录 $ ssh git@fooleap.org 'mkdir .ssh' $ ssh git@fooleap.org 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub * 在服务器的 ~/.ssh/authorized_keys 文件添加本地公钥 可以在本地的 ~/.ssh/config 文件定义 host git 对应的主机名及用户名
~/.ssh/config
host git
hostname fooleap.org
user git
经过这样处理之后,我们就可以通过 ssh git 自动登录服务器
## 服务器端
$ mkdir web.git && cd web.git
$ git --bare init
* 初始化,添加 bare 参数是为了创建一个裸仓库,即不包含工作目录
./hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=~/web git checkout -f
$ chmod +x hooks/post-receive
* 在挂钩目录 hooks 下创建一个 post-receive 文件,并赋予它可执行权限
**这么做的作用是什么呢?**
当我们在本地执行推送后,会自动执行这个脚本,将 ~/web 这个目录作为工作目录并进行 checkout 操作
$ cd && mkdir web
* 创建工作目录,这个目录可通过 Nginx 等设置对其访问
为用户 git 指定一个专用的 shell
/etc/passwd
- git:x:1001:1000:,,,:/home/git:/bin/bash
+ git:x:1001:1000:,,,:/home/git:/usr/bin/git-shell
* 将默认的 shell: bash 替换为 git-shell
这是为多人管理网站做的准备,作为协作管理的权限
## 客户端
$ mkdir web && cd web
$ git init
$ echo "Hello World" > index.html
$ git add .
$ git commit -m "add index.html"
$ git remote add web git@fooleap.org:web.git
$ git push web +master:refs/heads/master