在遠程服務器上架設了一個http server,然后通過git在本地做開發時,不想每次都登錄到遠程服務器上做pull操作,這個需求可以通過git hooks特性來實現。
原理是每當有用戶更新代碼時,會觸發xxxx.git的hooks中的post-receive,然后把要執行的操作寫在post-receive中就可以了。
開始的想法是
#!/bin/sh cd /project/canyon git pull currTime=$(date "+%Y%m%d%H%M%S") echo $currTime >> /project/canyon/log
然后這種寫法會出現找不到git repository 的問題,原因是在執行git pull 時引入了$GIT_DIR變量
remtoe: fatal: Not a git repository: '.'
於是我們將post-receive改為
#!/bin/sh unset $(git rev-parse --local-env-vars) dir="/project/canyon/" logfile="/project/canyon/log" cd $dir git pull currTime=$(date "+%Y%m%d%H%M%S") echo $currTime >> $logfile
這樣每一次用戶pull成功都會將/project/canyon中的git倉庫更新