github是可以设置hooks的,看:在设置webhooks & services,可在Just the push event.是设定向你的服务器发请求,然后再做相应的处理。

https://help.github.com/articles/creating-webhooks

看文档:man githooks

NAME
githooks - Hooks used by Git

SYNOPSIS
$GIT_DIR/hooks/*

DESCRIPTION
Hooks are little scripts you can place in $GIT_DIR/hooks directory to trigger action at certain points. When git init is run, a handful of example hooks
are copied into the hooks directory of the new repository, but by default they are all disabled. To enable a hook, rename it by removing its .sample
suffix.

Note
It is also a requirement for a given hook to be executable. However - in a freshly initialized repository - the .sample files are executable by
default.

This document describes the currently defined hooks.

git hook 自动布署代码

假设你的项目也是跑在此台服务器上,那自动布署代码就很简单了,比如你的在线服务代码在 /var/www/demo 文件夹中。

/var/www/demo也要有写权限

你先初始化代码库:

$ git clone /opt/git/gitdemo /var/www/demo

然后你可以通过 git pull 来更新代码。

当然这样是手动了,我想要的是本地提交更新后,服务器能自动的 git pull代码到最新,于是我们就要借助 git hook了。

进入到 /opt/git/gitdemo 文件夹中,会发现 .git/hook 文件夹在里面,进入到 hook 中,里面有很多的 sample 脚本,这里我们只需要用到 post-update。

$ mv post-update.sample post-update $ vim post-update

可以看到里面其实就是一些shell脚本,你要做的就是把 git pull写进去。当用户提交后,便会调用post-update脚本的。

比如我在服务器的代码库更新后,要求对应的服务代码也要更新(进行pull操作),
则在bare仓库的hooks中的post-receive添加如下内容即可

#!/bin/sh
unset $(git rev-parse --local-env-vars)
cd WEB_DIR
git pull

这些脚本显然是可以做很多事的,只要你想得到,要了解各脚本何时调用,google吧。

注:服务器中与git用户有关的文件夹及文件,

$ chown -Rh git:git /your/git/dirs

另外文章:

最佳工具 git hook

post-update.sample 改名为post-update

然后加几行简单的代码就能实现你的需求了

例:

gitdir=/****

cd $gitdir

git checkout 对应分支

git pull

end...

git设置hooks 钩子的更多相关文章

  1. Git 设置 Hook

    Git 设置 hook Hook 就是钩子,在需要的时候调用,根据每个钩子脚本(函数)的返回值决定下一步的操作. 在使用 Git 的过程中,有时候需要定制 Git 以便满足实际的需求. 需求 在一个项 ...

  2. git 设置多项目实现多账号登陆

    9:45 2015/11/18git 设置多项目时实现多账号用户登陆git config --global user.name "your_name" git config --g ...

  3. vscode git设置

    vscode只能打开一下界面: 在setting.path增加git.path选项,再使用linux的方法配置路径,就是使用D:/../bin/git.exe而不是\\ 重启vscode,git设置即 ...

  4. cmd/git设置alias提高效率

    cmd设置alias 在cmd或者git中有有些命令是比较长的,却需要频繁的使用,那么我们就可以设置alias来简化操作,无形中减少大量的宝贵时间,具体步骤如下. 第一步: 创建cmd_alias.b ...

  5. 转:git设置过滤忽略的文件或文件夹

    from: https://www.cnblogs.com/foohack/p/4629255.html git设置过滤忽略的文件或文件夹   我们一般向代码仓库提交项目的时候,一般需要忽略编译生成的 ...

  6. git设置HTTP代理

    git设置HTTP代理 设置HTTP代理 如果公司使用代理,git就需要设置代理才能克隆远程仓库 执行下面两条语句 git config --global http.proxy 10.167.32.1 ...

  7. git设置core.autocrlf

    背景: 使用虚拟机共享windows文件夹,文件夹中用git clone 一个仓库.在linux下编辑文件,用git status发现几乎所有的文件都为修改状态.   原因: windows下和lin ...

  8. git 设置代理.

    git 设置代理:(因为网络有时太慢,需要用到 ss 代理..) git config --global http.proxy http://127.0.0.1:1080 取消 代理 git conf ...

  9. git设置全局和单个仓库账号和密码

    Git全局配置和单个仓库的用户名邮箱配置 配置全局仓库的账号和密码 git config --global user.name "userName" //你的用户名 git con ...

随机推荐

  1. 【BZOJ1968】【AHoi2005】COMMON约数研究

    Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input ...

  2. hdu1232 并查集

    1. hdu1232 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 3.总结:简单并查集 #include<iostream> # ...

  3. Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    Bootstrap 的 carousel.js 插件并没有支持手势. 3种解决方案 : jQuery Mobile (http://jquerymobile.com/download/) $(&quo ...

  4. Picture effect of JavaScript

    每隔一定时间跟换图片Img = new Array("image/2.jpg","image/1.jpg","image/3.jpg",&q ...

  5. 处理海量数据的高级排序之——归并排序(C++)

    代码实现                                                                                                 ...

  6. JQuery的动画及其幻灯片效果

    1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,简单介绍jQuery的显示和隐藏. <script type="text/javascript&q ...

  7. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  8. Linux下安装配置Apache+PHP+MariaDB

    一.安装apache 1.下载并安装apache yum install httpd 2.启动apache systemctl start httpd.service 3.停止apache syste ...

  9. 关于Jquery获取Table中td内的内容

    $(this).children().eq(1).text()获取的是显示的值$(this).children().eq(1).html()获取的是<td></td>之间的所有 ...

  10. strcat、strcpy、memcpy 的使用注意

    char *p = "hello";//5 个长度 ; //char *str = (char *)malloc(sizeof(char)*len); ] = "niha ...