59

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

我已经尝试将这些作为服务器上的接收后挂钩放入 shell 脚本中,但它似乎没有任何效果。我知道脚本正在运行,因为我在推送后看到“将更改推送到服务器”。这是接收后挂钩:

#!/bin/sh
git reset --hard HEAD
echo "Changes pushed to server."
4

6 回答 6

73

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

简而言之,您要做的是向裸存储库添加一个“分离的工作树”。通常你认为你的工作树包含.git目录。根据定义,裸存储库没有工作树,但您可以创建一个,只要它位于与裸存储库不同的目录中即可。

post-receive 挂钩只是git checkout -f将存储库复制HEAD到工作目录中的简单方法。Apache 使用它作为它的文档根目录,你已经准备好了。每当您推送到裸存储库时,Apache 都会立即开始提供服务。

我通常使用它来自动推送到登台服务器,以查看“真实”环境是否会因我的更改而呕吐。部署到实时服务器是一个完全不同的故事。:-)

于 2010-10-01T11:40:57.403 回答
15

2015 年 3 月更新

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

git config receive.denyCurrentBranch updateInstead

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

这将允许您避免任何接收后挂钩。


(原始答案:2010 年 10 月)

GitFAQ建议非裸仓库使用此更新后挂钩:( 它可能会为您提供有关挂钩执行中实际发生的情况的更多线索。请注意,这是更新后挂钩,而不是接收后)

#!/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

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

git config receive.denyCurrentBranch ignore

或者

git config receive.denyCurrentBranch warn
于 2010-10-01T11:39:38.733 回答
11

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

sudo chmod +x hooks/post-receive

我们错过了一个sudo权限首先配置的东西。

于 2012-01-04T00:55:44.413 回答
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
于 2012-07-27T17:00:53.063 回答
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

允许推送到此存储库,尽管它不是裸露的:

git config receive.denycurrentbranch false
于 2013-07-10T18:35:27.057 回答
1

我只是在猜测,但这可能是一些权限问题(需要完整路径cd??)。检查日志文件中实际发生的情况。

然而,通过 git 发布文件始终只是发布过程的一项任务。您通常需要复制一些文件、删除其他文件、设置、更新权限、生成文档等。

对于复杂的解决方案,构建脚本可能比任何 git 钩子都要好。可以很好地处理这些任务的工具:

(我意识到这不是您期望的答案,但是作为评论发布太长了)

于 2010-10-01T13:05:20.183 回答