git 使用钩子直接推送到工作目录

远端机器

$ mkdir /www/teacherapi  # 创建工作目录

$ cd /data/git

$ git init teacherapi.git --bare --shared

Initialized empty shared Git repository in /data/git/teacherapi.git/

$ cd teacherapi.git/

$ vi hooks/post-receive # 或者使用 $ cat > hooks/post-receive 创建并键入如下代码,最后ctrl+d保存退出,

#!/bin/sh

GIT_WORK_TREE=/www/teacherapi git checkout -f <branch>  # 默认master分支

$ chmod +x hooks/post-receive  # 设置可执行权限

也可以用下面代码片段

$ cd git

$ mkdir somesite.git

$ cd somesite.git/

$ git init --bare

Initialized empty Git repository in /home/caius/git/somesite.git/

$ git --bare update-server-info

$ git config core.worktree /home/caius/vhosts/somesite.com/htdocs

$ git config core.bare false

$ git config receive.denycurrentbranch ignore

$ cat > hooks/post-receive

#!/bin/sh

git checkout -f

^D

$ chmod +x hooks/post-receive

工作树更新完成后执行脚本

$ cd /gitpah/hooks/

$ vi post-checkout  #创建post-checkout键入如下代码,当'git-checkout'命令更新完整个工作树(worktree)后,这个钩子就会被调用

#!/bin/sh

/etc/init.d/teacherapitest.tbkt.cn restart

$ chmod +x post-checkout

本地机器

$ git clone ssh://root@192.168.0.106:6022/data/git/teacherapi.git

$ cd teacherapi/

$ echo 'Hello, world!' > index.html

$ git add index.html

$ git commit -m "init project"

$ git push origin master:master

添加远程仓库

$ git remote add 106 ssh://root@192.168.0.106:6022/opt/gitdata/chsy_cms.git

远程推送过程中报错:cannot run hooks/post-receive: No such file or directory

除了检查post-receive文件内容外,还要看看文件格式对不对,使用$ cat -A hook/post-receive 查看

文档内容参考:

http://caiustheory.com/automatically-deploying-website-from-remote-git-repository

http://toroid.org/ams/git-website-howto

更多钩子用法:http://www.360doc.com/content/12/1012/11/10140166_241004726.shtml

原文地址:https://www.cnblogs.com/weiok/p/4772772.html