用于网站登台的Git Post-Receive Hook

Mat*_*att 59 git git-post-receive

我正在尝试设置Git来暂存我的网站,以便我可以git pull让当前版本在本地工作,然后git push将更改推送到远程服务器.我已经设置它以便按照我想要的方式工作,但是在我推动之后,我必须手动运行git checkout -fgit reset --hard HEAD在远程服务器上运行.

我已经尝试将这些作为服务器上的post-receive挂钩添加到shell脚本中,但它似乎没有任何效果.我知道脚本正在运行,因为我在推送后看到"更改推送到服务器".这是post-receive钩子:

#!/bin/sh
git reset --hard HEAD
echo "Changes pushed to server."
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 73

你的问题的答案在这里:http: //toroid.org/ams/git-website-howto

简而言之,您要做的是在裸存储库中添加"分离的工作树".通常,您将工作树视为包含.git目录.根据定义,裸存储库没有工作树,但只要它位于与裸存储库不同的目录中,您就可以创建工作树.

post-receive钩子很简单,git checkout -f可以将存储库复制HEAD到工作目录中.Apache使用它作为它的文档根目录,你们都已经设置好了.无论何时推送到裸存储库,Apache都会立即开始提供服务.

我通常使用它来自动推送到临时服务器,以查看"真实"环境是否会对我的更改产生影响.部署到实时服务器是一个完全不同的故事.:-)

  • [以网络为中心的Git工作流程](http://joemaller.com/990/a-web-focused-git-workflow/)是另一篇文章略有不同的好文章. (2认同)

Von*_*onC 15

2015年3月更新

正如我在" 将更改推送到远程存储库时,这个Git警告消息是什么? "中提到的,您实际上可以直接推送到非裸机库(Git 2.3.0 +,2015年2月),其中:

git config receive.denyCurrentBranch updateInstead
Run Code Online (Sandbox Code Playgroud)

相应地更新工作树,但如果有任何未提交的更改,则拒绝这样做.

这样可以避免任何后接收挂钩.


(原答案:2010年10月)

GitFAQ建议非裸露回购此更新后钩:
(它可能给你更多的线索,以什么在钩执行实际上是怎么回事注意,这是一个post-update挂钩,而不是后领取.)

#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".

git-update-server-info

is_bare=$(git-config --get --bool core.bare)

if [ -z "$is_bare" ]
then
    # for compatibility's sake, guess
    git_dir_full=$(cd $GIT_DIR; pwd)
    case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi

update_wc() {
    ref=$1
    echo "Push to checked out branch $ref" >&2
    if [ ! -f $GIT_DIR/logs/HEAD ]
    then
        echo "E:push to non-bare repository requires a HEAD reflog" >&2
        exit 1
    fi
    if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
    then
        wc_dirty=0
    else
        echo "W:unstaged changes found in working copy" >&2
        wc_dirty=1
        desc="working copy"
    fi
    if git diff-index --cached HEAD@{1} >/dev/null
    then
        index_dirty=0
    else
        echo "W:uncommitted, staged changes found" >&2
        index_dirty=1
        if [ -n "$desc" ]
        then
            desc="$desc and index"
        else
            desc="index"
        fi
    fi
    if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
    then
        new=$(git rev-parse HEAD)
        echo "W:stashing dirty $desc - see git-stash(1)" >&2
        ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
        git-update-ref --no-deref HEAD HEAD@{1}
        cd $GIT_WORK_TREE
        git stash save "dirty $desc before update to $new";
        git-symbolic-ref HEAD "$ref"
        )
    fi

    # eye candy - show the WC updates :)
    echo "Updating working copy" >&2
    (cd $GIT_WORK_TREE
    git-diff-index -R --name-status HEAD >&2
    git-reset --hard HEAD)
}

if [ "$is_bare" = "false" ]
then
    active_branch=`git-symbolic-ref HEAD`
    export GIT_DIR=$(cd $GIT_DIR; pwd)
    GIT_WORK_TREE=${GIT_WORK_TREE-..}
    for ref
    do
        if [ "$ref" = "$active_branch" ]
        then
            update_wc $ref
        fi
    done
fi
Run Code Online (Sandbox Code Playgroud)

为此,您仍然需要使用以下任一配置设置专门允许将更改推送到当前分支:

git config receive.denyCurrentBranch ignore
Run Code Online (Sandbox Code Playgroud)

要么

git config receive.denyCurrentBranch warn
Run Code Online (Sandbox Code Playgroud)

  • +1:剧本可能看起来笨重或冗长,但这是有充分理由的; 与使用普通`git reset --hard`或`git checkout -f`的直接方法不同,它将保留存储中的任何未提交的更改. (4认同)

ido*_*ido 11

我有完全相同的问题.在回复此链接时:http://toroid.org/ams/git-website-howto - 以下命令已完成:

sudo chmod +x hooks/post-receive
Run Code Online (Sandbox Code Playgroud)

我们错过了sudo首先配置这些东西的权限.


Tro*_*nic 6

固定版本的VonC脚本,适合我(绝对没有保证).

#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".

set -e

git update-server-info

is_bare=$(git config --get --bool core.bare)

if [ -z "${is_bare}" ]
then
    # for compatibility's sake, guess
    git_dir_full=$(cd $GIT_DIR; pwd)
    case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi

update_wc() {
    ref=$1
    echo "Push to checked out branch $ref" >&2
    if [ ! -f ${GIT_DIR}/logs/HEAD ]
    then
        echo "E:push to non-bare repository requires a HEAD reflog" >&2
        exit 1
    fi
    if (cd ${GIT_WORK_TREE}; git diff-files -q --exit-code >/dev/null)
    then
        wc_dirty=0
    else
        echo "W:unstaged changes found in working copy" >&2
        wc_dirty=1
        desc="working copy"
    fi
    if git diff-index --cached HEAD@{1} >/dev/null
    then
        index_dirty=0
    else
        echo "W:uncommitted, staged changes found" >&2
        index_dirty=1
        if [ -n "$desc" ]
        then
            desc="$desc and index"
        else
            desc="index"
        fi
    fi
    if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
    then
        new=$(git rev-parse HEAD)
        echo "W:stashing dirty $desc - see git-stash(1)" >&2
        ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
        git update-ref --no-deref HEAD HEAD@{1}
        cd ${GIT_WORK_TREE}
        git stash save "dirty $desc before update to $new";
        git symbolic-ref HEAD "$ref"
        )
    fi

    # eye candy - show the WC updates :)
    echo "Updating working copy" >&2
    (cd ${GIT_WORK_TREE}
    git diff-index -R --name-status HEAD >&2
    git reset --hard HEAD
    # need to touch some files or restart the application? do that here:
    # touch *.wsgi
    )

}

if [ x"${is_bare}" = x"false" ]
then
    active_branch=$(git symbolic-ref HEAD)
    export GIT_DIR=$(cd ${GIT_DIR}; pwd)
    GIT_WORK_TREE="${GIT_DIR}/.."
    for ref in $(cat)
    do
        if [ x"$ref" = x"${active_branch}" ]
        then
            update_wc $ref
        fi
    done
fi
Run Code Online (Sandbox Code Playgroud)


Hon*_*nza 6

用于设置此git部署的简单脚本:

准备收件后挂钩:

echo '#!/bin/sh'        >  .git/hooks/post-receive
echo 'git checkout -f'  >> .git/hooks/post-receive
echo 'git reset --hard' >> .git/hooks/post-receive
chmod +x .git/hooks/post-receive
Run Code Online (Sandbox Code Playgroud)

允许推入此存储库,虽然它不是裸:

git config receive.denycurrentbranch false
Run Code Online (Sandbox Code Playgroud)

  • 您还可以在结帐前添加`git diff -R --cached --name-status`,以获得推送方面正在更新的文件的清单. (3认同)