參考並轉載自:html
http://www.embbnux.com/2014/09/05/git_server_let_code_auto_deploy/git
http://www.chenyudong.com/archives/git-sync-manage-website.html#iweb
1. 建立遠程倉庫bash
$ mkdir /home/www.cnblogs.com/www.cnblogs.com.git #未來網站git的記錄存放位置 $ chown -R git:git www.cnblogs.com.git #改權限 $ cd www.cnblogs.com.git $ git init --bare Initialized empty Git repository in /home/www.cnblogs.com/www.cnblogs.com.git/
2. 在git遠端倉庫的hooks目錄下新建post-receive文件:post
#!/bin/sh #author: embbnux #Blog of Embbnux: http://www.embbnux.com #判斷是否是遠端倉庫 IS_BARE=$(git rev-parse --is-bare-repository) if [ -z "$IS_BARE" ]; then echo >&2 "fatal: post-receive: IS_NOT_BARE" exit 1 fi unset GIT_DIR DeployPath="/var/web" echo "===============================================" cd $DeployPath echo "deploying the test web" git fetch --all git reset --hard origin/master time=`date` echo "web server pull at webserver at time: $time." echo "================================================"
保存後賦予可執行權限:fetch
chmod +x hooks/post-receive