我正在尝试设置git,以便在我的网站上登录,以便我可以 git pull 要使当前版本在本地使用,然后 git push 将更改推向远程服务器。我已经设置了它,以便它按照我想要的方式工作,但是在推动之后,我必须手动运行 git checkout -f 或者 git reset --hard HEAD 在远程服务器上。

我尝试将它们作为服务器上的后接收钩将其放入Shell脚本中,但似乎没有任何效果。我知道脚本正在运行,因为我在推送后看到了“被推到服务器”。这是后接收的钩子:

#!/bin/sh
git reset --hard HEAD
echo "Changes pushed to server."
有帮助吗?

解决方案

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

简而言之,您想做的就是在裸仓库中添加“独立的工作树”。通常,您认为您的工作树包含 .git 目录。裸露的存储库从定义上没有工作树,但是只要它与裸仓库不同,您就可以创建一个。

后接管钩只是一个简单的 git checkout -f 复制存储库的 HEAD 进入工作目录。 Apache将其用作文档根,您全部设置。每当您推到裸仓库时,Apache都会立即开始使用它。

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

其他提示

2015年3月更新

正如我在将更改转换为远程存储库时,此GIT警告消息是什么?“,你实际上可以推 直接地 现在使用非抛售存储库(GIT 2.3.0+,2015年2月)。

git config receive.denyCurrentBranch updateInstead

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

这将使您避免任何后接收钩。


(原始答案:2010年10月)

Gitfaq 推荐 非Bare Repo 这个后期的挂钩:
(这可能会给您更多有关挂钩执行中实际发生的事情的线索。请注意,这是上升后的钩子,而不是后接收器)

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

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

sudo chmod +x hooks/post-receive

我们错过了一个 sudo 权限首先配置了这些内容。

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

用于设置此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

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

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

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

(我意识到这不是您期望的答案,但要发表评论太久了)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top