简体   繁体   English

Git推送到实时服务器

[英]Git push to live server

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 admin@webserver.example
webserver: password: ********
webserver: cd ~/domain.example/
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: ********

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.是的,您可以直接推送到您的网络服务器,但我不推荐它,因为您应该只推送到使用 --bare 参数克隆的存储库。 I'd utilize the Git hook system to let the main repository automatically update the repo on the web server.我会利用 Git 挂钩系统让主存储库自动更新 Web 服务器上的存储库。 Check out the post-update hook in:查看更新后挂钩:

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 ~/example.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.这样您只需要专注于推送到中央服务器,而不必关心 Web 服务器,一旦推送完成,它将始终更新。 If you can automate something, then automate it :)如果你可以自动化某些东西,那么就让它自动化:)

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-key,这很简单):

http://bash.cyberciti.biz/security/expect-ssh-login-script/ http://bash.cyberciti.biz/security/expect-ssh-login-script/

I had the same query and not satisfied with the currently top-voted answer here, ended up following git-website-howto which 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. TL;DR, git init --bare在你的 web 服务器上创建一个新的 repo,你将从你的开发机器推送你的更改。 When the web repo receives your changes, it fires the post-receive hook which then copies the files to your web root.当 Web 存储库收到您的更改时,它会触发 post-receive 挂钩,然后将文件复制到您的 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.我喜欢这种方法,因为 post-receive 挂钩在您的服务器上完成工作,因此您的本地机器可以更快地推送并释放自己。 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.因此,您可以有一个名为production的分支来更新您的 Web 服务器,而您的 master 继续用于开发并链接到其他地方的 git 存储库。

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.注意:您需要在您的 Web 服务器存储库上运行git config receive.denycurrentbranch ignore以在推送时抑制本地开发框上的警告。

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 URL 部分

so you would try:所以你会尝试:

git push ssh://admin@webserver.example/~admin/domain.example/ master

ADDED: I think part of what you are asking for is how to have multiple remote repositories.补充:我认为您所要求的部分内容是如何拥有多个远程存储库。

git remote add webserver ssh://admin@webserver.example/~admin/domain.example/

that allows you to run:允许您运行:

   git push origin master
   git push webserver master

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您可以将网络服务器添加为远程服务器,就像您做其他任何事情一样:

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
  1. Add remote $ git remote add server ssh://server_hostname:/path/to/git/repo添加远程$ git remote add server ssh://server_hostname:/path/to/git/repo
  2. Checkout temp branch on server $ git checkout -b temp服务器上的结帐临时分支$ git checkout -b temp
  3. Push changes $ git push server推送更改$ git push server
  4. Checkout previous branch and delete the temporary one $ git checkout - # shorthand for previous branch, git checkout @{-1} $ git branch -d temp签出上一个分支并删除临时分支$ 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

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 更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM