是否可以使用git push部署一个网站?我有一种预感,它与使用git挂钩来执行git重置有关——在服务器端很难,但我该如何完成这一点呢?


当前回答

git配置——local receive.denyCurrentBranch updateInstead

在Git 2.3中添加了一个很好的可能性:https://github.com/git/git/blob/v2.3.0/Documentation/config.txt#L2155

您在服务器存储库上设置它,如果工作树是干净的,它也会更新工作树。

在2.4中有进一步的改进,包括push-to-checkout钩子和未出生分支的处理。

示例用法:

git init server
cd server
touch a
git add .
git commit -m 0
git config --local receive.denyCurrentBranch updateInstead

cd ..
git clone server local
cd local
touch b
git add .
git commit -m 1
git push origin master:master

cd ../server
ls

输出:

a
b

这确实有以下GitHub公告中提到的缺点:

Your server will contain a .git directory containing the entire history of your project. You probably want to make extra sure that it cannot be served to users! During deploys, it will be possible for users momentarily to encounter the site in an inconsistent state, with some files at the old version and others at the new version, or even half-written files. If this is a problem for your project, push-to-deploy is probably not for you. If your project needs a "build" step, then you will have to set that up explicitly, perhaps via githooks.

但是所有这些点都超出了Git的范围,必须由外部代码来处理。因此,从这个意义上说,这和Git钩子一起是最终的解决方案。

其他回答

从本质上讲,你所需要做的就是:

server = $1
branch = $2
git push $server $branch
ssh <username>@$server "cd /path/to/www; git pull"

我在我的应用程序中有这些行,作为一个名为deploy的可执行文件。

所以当我想要部署时,我输入。/deploy myserver mybranch。

对于有多个开发人员访问同一个存储库的环境,下面的指导方针可能会有所帮助。

确保你有一个所有开发人员都属于的unix组,并将.git存储库的所有权授予该组。

在服务器存储库的.git/config中设置sharerepository = true。(这告诉git允许提交和部署所需的多个用户。 将每个用户的bashrc文件中的umask设置为相同的- 002是一个很好的开始

我在这个网站上找到了这个脚本,它似乎工作得很好。

复制你的。git目录到你的web服务器 在你的本地副本上,修改你的.git/config文件,并添加你的web服务器作为远程服务器: (远程“生产”) Url = username@webserver:/path/to/htdocs/.git 在服务器上,用这个文件替换。git/hooks/post-update(在下面的答案中) 添加对文件的执行访问(同样是在服务器上): Chmod +x .git/hooks/post-update 现在,只要本地推送到你的web服务器,它就会自动更新工作副本: Git推送生产

You could conceivably set up a git hook that when say a commit is made to say the "stable" branch it will pull the changes and apply them to the PHP site. The big downside is you won't have much control if something goes wrong and it will add time to your testing - but you can get an idea of how much work will be involved when you merge say your trunk branch into the stable branch to know how many conflicts you may run into. It will be important to keep an eye on any files that are site specific (eg. configuration files) unless you solely intend to only run the one site.

或者,你有没有考虑过把改变推到网站上?

有关git钩子的信息,请参阅githooks文档。

我使用toroid.org的以下解决方案,它有一个更简单的钩子脚本。

服务器端:

$ mkdir website.git && cd website.git
$ git init --bare
Initialized empty Git repository in /home/ams/website.git/

并在服务器上安装钩子:

$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
GIT_WORK_TREE=/var/www/www git clean -f -d # clean directory from removed files

$ chmod +x hooks/post-receive

在您的客户端:

$ mkdir website && cd website
$ git init
Initialized empty Git repository in /home/ams/website/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "The humble beginnings of my web site."

$ git remote add web ssh://server.example.org/home/ams/website.git
$ git push web +master:refs/heads/master

然后要发布,只需输入

$ git push web

网站上有完整的描述:http://toroid.org/ams/git-website-howto