# 在EC2上创建Git仓库
## 1. 创建主仓库
在EC2上建立一个中央仓库。SSH到EC2实例后
``` bash
mkdir repo-name.git
cd repo-name.git
git init --bare
```
一个bare仓库是不能进行commit的,只是当作一个中央仓库存在。所有工作都应该先在本地的仓库中完成后push进来。
---
## 2. 在本地push/pull
本地git仓库是通过ssh从服务器push/pull的,又因为EC2必须有私钥,所以先执行一步添加私钥的操作。
``` bash
eval "$(ssh-agent -s)"
ssh-add /path/to/the/private/key
```
然后在本地建立另一个工作仓库,
``` bash
cd repo-name
git init
git add .
git commit -m "Initial commit"
git remote add origin ubuntu@xxxxxxx:/path/to/your/repo-name.git
git config --global remote.origin.receivepack "git receive-pack" # needed for EC2 stuff
git push origin master
```
以后工作仓库的改动都可以push到中央仓库了。
---
## 3. 自动更新这个网站
在本地更新了网站内容后,源代码会push到服务器的仓库里。我新建了一个脚本,可以一键完成
(服务器端同步更新源码 --- 在服务器端编译 --- 部署到nginx根目录下)这一系列动作。
``` bash
git pull origin master
source .venv/bin/activate
sphinx-build source notes
sudo rm -rf /var/www/html/notes
sudo cp -r notes /var/www/html/
deactivate
```
---
## 参考资料