Git post-receive hook does not delete deleted files from master

I created a bare git repository on my server and configured the following post-receive hook from this blog :

#!/bin/bash while read oldrev newrev ref do branch=`echo $ref | cut -d/ -f3` if [ "master" == "$branch" ]; then git --work-tree=/path/to/my/project/live/ checkout -f $branch echo 'Changes pushed live.' fi if [ "develop" == "$branch" ]; then git --work-tree=/path/to/my/project/dev/ checkout -f $branch echo 'Changes pushed to dev.' fi done 

Thus, whenever I click locally on my server, the changes will be automatically published in each folder of the branch without the need for manually.

I set the correct permissions to work with the dev folder:

 drwxrwsr-x 2 git git 4096 Sep 29 12:10 live/ drwxrwsr-x 2 git git 4096 Sep 29 12:09 dev/ 

And pushing away from the development branch works as expected. The problem arises when I check the master branch and do a merge. When I click the wizard, new files are copied to a live folder on my server, but files that I delete locally are not deleted.

How can I make post-receive correctly update the current folder? Thanks!

+2
git git-post-receive
source share
2 answers

The problem is that git does not know what to delete (it does not have an index in the work tree, tracking such things). It should be possible to resolve this with an index for each work tree, but I think it’s easier just to git checkout -f into a new empty directory, and then rename the new directory and the old one so that the new version of “go live”, This also shortens the race status window : now there is only one short moment (between mv operations) when there is no version, and not a slightly longer window (during checkout ) when there is a combination of old and new versions.

Note. You show that the script will look too much if there is a tag named master or develop , since the link names for these two are refs/tags/master and refs/tags/develop respectively. I would recommend installing this (if you are interested :-)) through the shell function and case , in order to reduce the spawning process in cases not related to deployment, for example:

 die() { echo " $@ " >&2 exit 1 } # deploy (to given path, $1) the version named by $2 # if the target is /some/path/there we use a temp version # named /some/path/tmp.<pid> to hold the new one until we # can swap it out, and $1.old.<pid> while we remove the old. deploy() { local path=$1 branch=$2 local tmpdir=${path%/*}/tmp.$$ # tune this as needed echo "deploying $branch to $path via $tmpdir..." trap "rm -rf $tmpdir" 0 1 2 3 15 mkdir $tmpdir || die "can't create work dir $tempdir" git --work-tree=$tmpdir/ checkout -f $branch mv $path $path.old.$$ || die "unable to move live version out of the way" mv $tmpdir $path || die "unable to set new version live" trap - 0 1 2 3 15 echo "done, cleaning up old version" rm -rf $path.old.$$ } while read oldrev newrev ref; do case $ref in refs/heads/master) deploy /path/to/my/project/live master;; refs/heads/develop) deploy /path/to/my/project/dev develop;; esac done 

(note: fully unverified).

+3
source share

I ran into the same problem. I studied rsync to copy tmp folder files to a live folder, but then I realized why it's not easy to use git glean on the working tree.

I'm not sure if this was bad, but it should delete unnecessary files from the folder and use your .gitignore settings so as not to delete files not included in your repo.

It seems to me that I want to delete the remaining files that were not deleted by clicking.

 #!/bin/sh while read oldrev newrev refname do branch=$(git rev-parse --symbolic --abbrev-ref $refname) if [ "master" == "$branch" ]; then GIT_WORK_TREE=/home/store/public_html git checkout -f master GIT_WORK_TREE=/home/store/public_html git clean -fd fi if [ "test" == "$branch" ]; then GIT_WORK_TREE=/home/store/test_html git checkout -f test GIT_WORK_TREE=/home/store/test_html git clean -fd fi if [ "dev" == "$branch" ]; then GIT_WORK_TREE=/home/store/dev_html git checkout -f dev GIT_WORK_TREE=/home/store/dev_html git clean -fd fi done 
+2
source share

All Articles