使用单个命令 (git Push) 更新网站,而不是 FTP 拖放
情况:
- 我有一个网站的本地副本
- 我有一个可以通过 SSH 访问的服务器
我想做什么?
- 本地提交,直到我对代码感到满意为止
- 在本地创建分支
- 有一个主分支,该分支应该推送到服务器
- 使用单个命令更新网站 (git push origin master)
如果我使用本地设置 git 存储库git init,然后push到服务器上的某个文件夹,不起作用。 当我通过 FTP 到服务器检查文件时,它们实际上就在那里。 当我通过 SSH 连接到服务器并执行 git status 时,它并不干净,尽管它应该是干净的,因为我刚刚推送到服务器。
我正在做的步骤:
- 在我的计算机上创建一个新文件夹(mkdirfolder_x)
- 进入该文件夹(cdfolder_x)
- 在那里设置一个新的git存储库(gitinit)
- (git存储库设置成功)
- 使用将存储库推送到服务器git push origin master(其中 origin 设置为用户:[电子邮件受保护])
Situation:
- I have a local copy of a website
- I have a server that I have SSH access to
What do I want to do?
- Commit locally until I'm happy with my code
- Make branches locally
- Have one master branch that is the one that should be pushed to the server
- Update the website using a single command (git push origin master)
If I set up a git repo locally using git init, and then push to a folder on the server, it doesn't work. When I FTP to the server to check the files, they're actually there. When I SSH into the server and do git status, it's not clean, even though it should be since I just pushed to the server.
Steps I'm doing:
- Make a new folder on my computer (mkdir folder_x)
- Go into that folder (cd folder_x)
- Set up a new git repository there (git init)
- (git repository sets up successfully)
- Push the repository to the server using git push origin master (where origin is set up as user:[email protected])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要编写一个接收后钩子,在远程 git 存储库收到推送后运行 git checkout -f 。 有关详细信息,请参阅使用 Git 管理网站。
You need to write a post-receive hook that runs
git checkout -f
after the remote git repository receives a push. See Using Git to manage a web site for details.当您推送到 git 存储库时,它不会更新该存储库的工作文件。 一般来说,当你推送时,你应该推送到一个裸存储库——这似乎就是他们希望 git Push 工作的方式。
对于我的一个(本地)项目,我编写了一个脚本,当我将工作推送到部署存储库时,自动检查最新的“ui”子目录。 它嵌入在以前的答案中:使用 Git 开发 Django 项目
如果有人将更新推送到 master,也可以简单地让服务器上的更新后挂钩执行“git reset --hard master”,并使用非裸存储库,而不是为签出文件提供单独的区域。
When you push to a git repository, it doesn't update that repository's working files. Generally when you push, you should be pushing to a bare repository--- that seems to be how they intended git push to work.
For one of my (local ) projects, I wrote a script to automatically check out the latest "ui" subdirectory when I push my work to a deployment repo. It's embedded in a previous answer here: Developing Django projects using Git
You could also simply have the post-update hook on the server do a "git reset --hard master" if someone pushes an update to master, and use a non-bare repo rather than having a separate area for checked-out files.
我使用更新后挂钩,如常见问题解答条目中所述:
http://git.or.cz/gitwiki/GitFaq#non-bare
如果您已将远程存储库配置为“裸”,它将更新您的工作树。
I use the post-update hook as detailed in this FAQ entry:
http://git.or.cz/gitwiki/GitFaq#non-bare
It will update your working tree if you have configured the remote repo as "bare".
Git 版本 1.9.1
Ubuntu 服务器 14.04 LTS
LAMP 服务器
我将 LAMP 服务器设置为每当我的一位 Web 开发人员将更改推送到服务器时更新我的 Git 存储库的工作目录。 我注意到日志会记录新的提交,但不会更新工作目录。 无需为每次更新手动执行此操作(git checkout -f),而是可以在收到推送后自动设置为执行此操作。
在“hooks”文件夹中创建一个名为“post-receive”的文件,其中包含以下内容:
#!/bin/sh
# 收到远程客户端的推送后更新工作目录。
# 这应该指向 git 工作目录。
GIT_WORK_TREE=/var/www/dev_site git checkout -f
通过在“hooks”文件夹中键入“chmod +x post-receive”来启用执行文件的权限。< /p>
现在,当提交推送到 Git 存储库时,它会更新工作目录。 现在,当我在浏览器中访问我的网站时,它会显示更改。
在此示例中,我的工作目录是“/var/www/dev_site”。 根据您的存储库所在位置进行相应更改。
请注意,这个 Repo 不是一个裸露的 Repo。 裸露的 Repo 没有 Web 服务器可以读取的工作目录。
Git Version 1.9.1
Ubuntu Server 14.04 LTS
LAMP Server
I set my LAMP server to update my working directory of my Git repo whenever one of my web developers pushes a change to the server. I noticed that the log would note the new commits, but would not update the working directory. Instead of doing this manually (git checkout -f) for every update, this can be set automatically to do so after a push has been received.
Create a file named "post-receive" within the "hooks" folder with this content:
#!/bin/sh
# Update working directory after receiving a push from remote clients.
# This should be directed at the git working directory.
GIT_WORK_TREE=/var/www/dev_site git checkout -f
Enable permissions to execute the file by typing "chmod +x post-receive" in the "hooks" folder.
It will now update the working directory when commits are pushed to the Git repo. My site now shows the changes when I visit it in a browser.
In this example, my working directory is "/var/www/dev_site". Change this accordingly to where your Repo is.
Please note, this Repo is not a bare Repo. A bare Repo does not have a working directory a web server can read from.