git Git推送到实时服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3728054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git push to live server
提问by Petah
We have a website that has all its PHP/HTML/JS/CSS/etc files stored in a Git repository.
我们有一个网站,它的所有 PHP/HTML/JS/CSS/etc 文件都存储在 Git 存储库中。
We currently have 3 types of computers (or use cases) for the repository.
我们目前有 3 种类型的计算机(或用例)用于存储库。
- Local developer: pull latest changes, make changes, commit to local repo, push to master server
- Master server: central repository, all changes get pushed to the master server
- Web server: changes are pulled down from the master server when deploying the website
- 本地开发人员:拉取最新更改,进行更改,提交到本地存储库,推送到主服务器
- 主服务器:中央存储库,所有更改都推送到主服务器
- Web 服务器:在部署网站时从主服务器拉下更改
So currently we:
所以目前我们:
local: git push origin master
local: password: ********
local: ssh [email protected]
webserver: password: ********
webserver: cd ~/domain.com/
webserver: git pull origin master
So my question is: is there a way that from my local computer I can push straight to the web server?
所以我的问题是:有没有办法从我的本地计算机直接推送到 Web 服务器?
ie.
IE。
local: git push origin master
local: password: ********
local: git push webserver master
local: password: ********
回答by ralphtheninja
Yes you can push directly to your webserver, but I wouldn't recommend it since you should only push to repositories cloned with the --bare argument. I'd utilize the git hook system to let the main repository automatically update the repo on the web server. Check out the post-updatehook in:
是的,您可以直接推送到您的网络服务器,但我不会推荐它,因为您应该只推送到使用 --bare 参数克隆的存储库。我会利用 git hook 系统让主存储库自动更新 Web 服务器上的存储库。在以下位置查看更新后挂钩:
http://git-scm.com/docs/githooks
http://git-scm.com/docs/githooks
This script could in turn login to the web server via ssh and do
该脚本可以依次通过 ssh 登录到 Web 服务器并执行
cd ~/domain.com/
git checkout master
git pull origin master
This way you only need to focus on pushing to the central server and don't have to care about the web server, it will always be updated once a push has been made. If you can automate something, then automate it :)
这样你只需要专注于推送到中央服务器,而不必关心 web 服务器,一旦推送完成,它总是会更新。如果你可以自动化一些东西,那就自动化它:)
I even found a nice article for you about logging in via ssh in a script (if you must use password, this is trivial if a ssh-key has been setup):
我什至为你找到了一篇关于在脚本中通过 ssh 登录的好文章(如果你必须使用密码,如果已经设置了 ssh 密钥,这很简单):
http://bash.cyberciti.biz/security/expect-ssh-login-script/
http://bash.cyberciti.biz/security/expect-ssh-login-script/
Hope this helps!
希望这可以帮助!
回答by aleemb
I had the same query and not satisfied with the currently top-voted answer here, ended up following git-website-howtowhich outlines the process fairly well and is IMO a much cleaner and quicker approach.
我有同样的问题,但对这里目前最高投票的答案不满意,最终遵循了git-website-howto,它很好地概述了该过程,并且 IMO 是一种更清晰、更快捷的方法。
TL;DR, git init --bare
to create a fresh repo on your web server where you will push your changes from your dev machine. When the web repo receives your changes, it fires the post-receive hook which then copies the files to your web root.
TL;DR,git init --bare
在你的 web 服务器上创建一个新的 repo,你将从你的开发机器推送你的更改。当 web repo 收到您的更改时,它会触发 post-receive hook,然后将文件复制到您的 web 根目录。
I like this approach because the post-receive hook does the work on your server so your local machine can push much faster and free itself up. This also makes it very easy to setup remote tracking for a particular branch. So you could have a branch called production
to update your web server, while your master continues to be for development and link to your git repo elsewhere.
我喜欢这种方法,因为 post-receive hook 在你的服务器上完成工作,所以你的本地机器可以更快地推送并释放自己。这也使得为特定分支设置远程跟踪变得非常容易。因此,您可以调用一个分支production
来更新您的 Web 服务器,而您的 master 继续用于开发并链接到其他地方的 git repo。
Note: you'll need run git config receive.denycurrentbranch ignore
on your web server repo to suppress a warning on your local dev box when pushing.
注意:您需要git config receive.denycurrentbranch ignore
在您的 Web 服务器存储库上运行以在推送时抑制本地开发框上的警告。
回答by Tim Hoolihan
Look at the git urls portion of http://www.kernel.org/pub/software/scm/git/docs/v1.6.0.6/git-push.html
查看http://www.kernel.org/pub/software/scm/git/docs/v1.6.0.6/git-push.html的 git urls 部分
so you would try:
所以你会尝试:
git push ssh://[email protected]/~admin/domain.com/ master
ADDED: I think part of what you are asking for is how to have multiple remote repositories.
添加:我认为您要求的部分内容是如何拥有多个远程存储库。
git remote add webserver ssh://[email protected]/~admin/domain.com/
that allows you to run:
这允许您运行:
git push origin master
git push webserver master
回答by cmcginty
I think the feature you are looking for is described here: http://debuggable.com/posts/git-tip-auto-update-working-tree-via-post-receive-hook:49551efe-6414-4e86-aec6-544f4834cda3
我认为您正在寻找的功能在这里描述:http: //debuggable.com/posts/git-tip-auto-update-working-tree-via-post-receive-hook: 49551efe-6414-4e86-aec6- 544f4834cda3
From local
you can add the webserver as a remote, just like you would do any other:
从local
您可以添加Web服务器为远程,就像你会做任何其他:
git remote add webserver admin@webserver:/path/to/repo.git/
# push only master branch by default
git config remote.webserver.push master
Now when your ready to push you can just do:
现在,当您准备好推动时,您可以执行以下操作:
git push webserver
回答by 2upmedia
- Add remote
$ git remote add server ssh://server_hostname:/path/to/git/repo
- Checkout temp branch on server
$ git checkout -b temp
- Push changes
$ git push server
- Checkout previous branch and delete the temporary one
$ git checkout - # shorthand for previous branch, git checkout @{-1} $ git branch -d temp
- 添加遥控器
$ git remote add server ssh://server_hostname:/path/to/git/repo
- 在服务器上结帐临时分支
$ git checkout -b temp
- 推送更改
$ git push server
- 签出上一个分支并删除临时分支
$ git checkout - # shorthand for previous branch, git checkout @{-1} $ git branch -d temp
I have more background information here: https://medium.com/@2upmedia/git-push-to-live-server-5100406a26
我这里有更多背景信息:https: //medium.com/@2upmedia/git-push-to-live-server-5100406a26
回答by Rohan Khude
Before deploying the local changes, check whether anything has changed on target server.
在部署本地更改之前,请检查目标服务器上是否有任何更改。
Add to deployment script to ensure nothing has changed on server:
添加到部署脚本以确保服务器上没有任何更改:
$ git ls-files -dmo --exclude-standard
Will be empty if there are unchanged files, easier than parsing git status
如果有未更改的文件,则为空,比解析 git status 容易