Git post-receive hook to checkout each branch to different folders?

泪湿孤枕 提交于 2019-11-29 02:21:12
torek

You're getting this because /master and /develop are non-existent directories:

$ git --work-tree=/nonexistent checkout master
fatal: This operation must be run in a work tree

You may also want to see my answer to a question about another problem that crops up with this approach, which also addresses a small bug you've copied from a popular-but-wrong post-receive technique (the use of cut to parse the updated ref).

[Your phrasing also makes me wonder if you are thinking of deploying those two branches into a sub-directory within the (presumably --bare) repository that is receiving the pushes. This is probably not a great idea.]

Another (different) method for deploying is to have a "real" git tree in the deployment location. Then, instead of git --work-tree=... checkout you do something like this instead:

deploy()
{
    local path=$1 branch=$2

    (cd $path && unset GIT_DIR && git fetch && git checkout -f origin/$branch)
}

(untested, feel free to experiment and/or modify). This has other, slightly different tradeoffs with respect to disk space and update windows (which I mention in the other answer).

you should put 'GIT_WORK_TREE=/master git checkout -f $branch' in one line, then it worked, after read your post, my post-receive is :

#!/bin/sh

while read oldrev newrev ref
do
  branch=`echo $ref | cut -d/ -f3`

  if [ "master" == "$branch" ]; then
    GIT_WORK_TREE=/home/tnj/www/www.test.com git checkout -f $branch
    chmod -R 775 /home/tnj/www/www.test.com/
    echo 'changes pushed to www.test.com'
  fi

  if [ "develop" == "$branch" ]; then
    GIT_WORK_TREE=/home/tnj/www/www.test.com.dev git checkout -f $branch
    chmod -R 775 /home/tnj/www/www.test.com.dev/
    echo 'changes pushed to www.test.com.dev'
  fi
done

it worked fine, thank you for your post ! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!