1.在服务器上建立裸仓库git
git init --bare
2.git裸仓库钩子在hooks/中
进入hooks中vim
$ cd git/hooks $ touch post-receive && vim post-receive
在 post-receive文件中添加一下内容:服务器
#!/bin/sh DEPLOY_PATH=/home/wwwroot/default/myproject/ #这个路径是服务器上项目的目录位置 unset GIT_DIR #这条命令很重要 cd $DEPLOY_PATH git reset --hard git pull chown root:root -R $DEPLOY_PATH
保存文件并修改权限:ssh
chmod +x post-receive
3.服务器上的项目目录克隆git上的项目地址(用ssh地址,须要把服务器的上公钥添加到git上)ide
4.在本身的电脑上,生成公钥,在服务器的~/.ssh/authorized_keys 的文件中添加上你本地电脑的公钥
5.在本地的仓库中使用命令:post
git remote add server ssh://root@<服务器ip地址>/<服务器git目录地址>
6.在本地修改好代码后,提交到远端,git push到master
7.执行git push server 远端自动拉取代码code