Managing website using git + post-receive hook: error pushing changes

我们两清 提交于 2019-11-29 15:47:22

Why not:

  • pushing your commits to a bare repo
  • setting a post-receive hook on that dev repo which will then go to the non-bare dev repo and checkout the right branch, pulling the new commits from the bare repo?

That is what your link describes (with the detached working tree), but it sets the hook in a bare repo:

$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
$ chmod +x hooks/post-receive

Working with repositories is not what you want to do. You need a post-receive hook in the bare repository on your dev server that does a git checkout-index to your web server's directory. That way, it's just files. You don't have to worry about whether it's a bare repository, has a working tree, or anything else git-related.

Something like git checkout-index -f -a --prefix=/var/www/htdocs/mywebsite/ (note the trailing slash - it is important)

The -f flag will force an overwrite of any existing files and the -a flag will include all files.

You just need to be careful about what you are trying to do.

git status is used for telling you the status of your working tree. This should not be useful in your case.

On the other had, git push is perfectly valid on a bare repository. This should just work for you.

If you are wanting to look at changes, you can with git diff. You just need to specify both hashes git diff $oldref $newref

If you want to list the files changed in a specific commit use git show --pretty="format:" --name-only $newref.

If you want to list the contents of a file use git show $newref -- $filepath

I've worked out a solution that is acceptable, but not quite ideal. Basically, I am just circumventing the problem by editing my config file when I need to push dev changes to my live server.

Config file which allows developers to push local changes to dev:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = true

Config file when pushing dev changes to live:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        worktree = /var/www/example.com/httpd/

Pros

  1. Works
  2. Once I have changed the config file to push to live, the repo is 'locked' and other developers cannot push changes
  3. Not a huge problem since I have to login to dev to handle database migration anyway

Cons: basically just that it's work that I'm sure could be automated, if I knew how to do it correctly.

Note: I like @vonC's suggestion of setting up the post-recieve hook to conditionally also push to the live server. Making the changes to the post-recieve hook is simple enough, but I do not understand what the trigger would be that the hook would respond to. I'd love to hear suggestions though : )

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