是否可以使用git push部署一个网站?我有一种预感,它与使用git挂钩来执行git重置有关——在服务器端很难,但我该如何完成这一点呢?
当前回答
我在这个网站上找到了这个脚本,它似乎工作得很好。
复制你的。git目录到你的web服务器 在你的本地副本上,修改你的.git/config文件,并添加你的web服务器作为远程服务器: (远程“生产”) Url = username@webserver:/path/to/htdocs/.git 在服务器上,用这个文件替换。git/hooks/post-update(在下面的答案中) 添加对文件的执行访问(同样是在服务器上): Chmod +x .git/hooks/post-update 现在,只要本地推送到你的web服务器,它就会自动更新工作副本: 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钩子一起是最终的解决方案。
作为补充的答案,我想提供一个替代方案。我使用git-ftp,它工作得很好。
https://github.com/git-ftp/git-ftp
使用方便,只需打字:
git ftp push
git会自动上传项目文件。
问候
我使用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
我在这个网站上找到了这个脚本,它似乎工作得很好。
复制你的。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文档。
推荐文章
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?
- 我如何从现有回购的分支创建一个新的GitHub回购?
- 如何将Git分支移到它自己的存储库中?
- 如何禁用Git凭证管理器的Windows?
- Git 拉取与 Git 变基
- 在git中压缩提交是什么意思?
- 在GIT中合并2个分支
- git标签也会被推送吗?
- 嵌套的Git存储库?
- Git bash错误:无法fork子进程:没有可用的终端(-1)
- 有没有办法在SourceTree中获得两个分支的视觉差异?
- Git标记是否只应用于当前分支?
- Git分支:master vs. origin/master vs. remotes/origin/master
- 如何根据文件扩展名过滤git差异?