1

For my website I have a bare repository (where I push to) and a clone with working directory which serves as document root.

In the bare repository I've setup a post-receive hook to auto-update the website:

#!/bin/sh
WEB_DIR=/var/www/www.mysite.com

# remove any untracked files and directories
git --work-tree=${WEB_DIR} clean -fd

# force checkout of the latest deploy
git --work-tree=${WEB_DIR} checkout --force

Unfortunately, after a push to the bare repository the changes do show up in the documentroot, but as but they show as unstaged changes. I then have to clean everything up by doing a git checkout -- <files> followed by a git pull.

Obviously the idea is that this works out of the box, if I have to do the cleanup+pull manually I might as well remove the post-receive hook.

I read it's better to use checkout in the post receive than a pull in various locations so I don't know if that would be a good change...

I'm not a git expert so I'm hoping someone can tell me what I'm missing? The solution does seem to work for a lot of people as I've found it in several tutorials out there.

4

1 回答 1

0

这令人困惑

所以现在你得到的是(例如):

/repos
    /mysite
        /.git 
            ...
/var
    /www
        /www.mysite.com
            /.git
                ...
            index.htm
            ...

那里没有什么奇怪的。然而,使用中的钩子不匹配,导致工作副本的 git 存储库与签出文件不同步:

/repos
    /mysite
        /.git <- This git dir
            ...
/var
    /www
        /www.mysite.com <- This working copy
            index.htm
            ...

工作副本的 git 目录被忽略。这让事情变得相当混乱——工作副本文件被签出以匹配裸存储库的状态,而忽略了工作副本自己的 git 信息。

让它变得简单

而不是这样做:

#!/bin/sh
# see http://serverfault.com/q/107608/108287
# for why you can't just run git pull with no arguments

WEB_DIR=/var/www/www.mysite.com

cd $WEB_DIR

# Remove the comments after verifying that it works to reduce noise
git --git-dir $WEB_DIR/.git fetch # > /dev/null
git --git-dir $WEB_DIR/.git checkout --force # > /dev/null
git --git-dir $WEB_DIR/.git clean -fd # > /dev/null

即推送到裸存储库,然后准确模拟您手动执行的操作,更改目录拉取和清理。这将更新工作副本的文件git 存储库,以使事情保持同步。

于 2014-06-20T09:08:53.027 回答