简体   繁体   English

将Git分支直接推送到实时服务器目录,以便实时查看文件

[英]Push Git branches directly into a live server directory so files are seen live

How can I set up remote directories in Git where I can locally push a stage branch to the remote and see the live changes on a staging server, like stage.example.com? 如何在Git中设置远程目录,我可以在本地将阶段分支推送到远程并查看登台服务器上的实时更改,例如stage.example.com?

The notion I have (part of why I am moving away from SVN) is that I can maintain (locally) 3 different "main" branches as follows: 我有这个概念(我离开SVN的部分原因)是我可以维护(本地)3个不同的“主”分支,如下所示:

  • master - used for local development, working dir master - 用于本地开发,工作目录
  • stage - should be in-sync with staging server directory (remote) 阶段 - 应与登台服务器目录(远程)同步
  • live - this should be the publicly accessible website (remote) 直播 - 这应该是可公开访问的网站(远程)

The idea I have (and what others claim is possible) is that I can maintain these remote "sites" from my local computer without having to constantly log into my remote server shell and run svn update (in my current svn workflow I need to do this all the time…) or of course in my Git workflow run git pull on the remote. 我的想法(以及其他人声称的可能)是我可以从本地计算机维护这些远程“站点”而无需经常登录我的远程服务器shell并运行svn update (在我当前的svn工作流程中我需要做的事情)这一直......)或者当然在我的Git工作流程中运行遥控器上的git pull

How can I setup the remote directories so that I can locally push my stage branch to the staging remote server and see the changes on (for example) stage.example.com right away? 如何设置远程目录以便我可以在本地将我的stage分支推送到staging remote server并立即查看(例如) stage.example.com上的更改?

Then once the stage is all okay and tested I would just locally be able to push to the live remote to make these changes that I have tested on the stage to the live website. 然后,一旦stage一切正常并经过测试,我就可以在本地能够push送到live遥控器,以便我在stage上测试这些更改到实时网站。

Can this even be done or am I getting crazy ideas here that are simply not meant to be done with Git? 甚至可以这样做,还是我在这里得到疯狂的想法,这些想法根本不是用Git完成的?

In case this is of importance here are a few stats about my local and remote servers: 如果这很重要,这里有一些关于我的本地和远程服务器的统计信息:

remote server:      Dreamhost (shared account)
remote GIT version: 1.7.1.1
remote GIT client:  shell

local computer:     Mac Pro (Snow Leopard 10.6.6)
local GIT version:  1.7.2.3
local GIT client:   Tower.app // git-tower.com

Also, so far I have unsuccessfully tried the following workflow: 此外,到目前为止,我没有成功尝试以下工作流程:

  1. Create a --bare Git repo on the remote (so I can access it from everywhere) 在遥控器上创建一个--bare Git --bare (所以我可以从任何地方访问它)
  2. Clone this remote repo to a local directory and the use Git Tower app to manage it 将此远程仓库克隆到本地目录,并使用Gi​​t Tower应用程序对其进行管理
  3. Work locally in master (HEAD) 在本地工作master (HEAD)
  4. scp -r copy the --bare git repo from the remote server into my remote live domain stage.example.com scp -r--bare git repo从远程服务器复制到我的远程实时域stage.example.com
  5. Add remote to local working copy and then try to push to origin/stage 将远程添加到本地工作副本,然后尝试推送到origin/stage

Clearly this doesn't work but I don't know why or how to do it any better. 显然,这不起作用,但我不知道为什么或如何做得更好。

Coming from a SVN background I'm new to Git but have watched plenty of tutorials (Peepcode & ThinkVitamin) but still cannot figure out how to set this up. 来自SVN背景我是Git的新手,但已经看过很多教程(Peepcode和ThinkVitamin),但仍然无法弄清楚如何设置它。

The one notion to realize with a DVCS ("Distributed" VCS, like Git or Mercurial) is that it adds the notion of publication (push/pull) to the notion of branching. 使用DVCS(“分布式”VCS,如Git或Mercurial)实现的一个概念是,它将发布 (推/拉)的概念添加到分支的概念中。
A CVCS ("Centralized" VCS, like SVN) has only branching (and one central repo to push to on a server). CVCS(“集中式”VCS,如SVN)仅具有分支(以及一个中央仓库以推送到服务器上)。

In your case, staging or live are publication steps, ie different Git repo ready to receive the modifications you want to see in staging or in live environment. 在您的情况下,staging或live是发布步骤,即不同的Git repo准备好接收您想要在分段或实时环境中看到的修改。

That would mean: 那意味着:

  • 2 branches to keep track of what belong to staging (" staging " branch) or live (" live " branch) 2个分支,以跟踪什么属于分期(“ staging ”分支)或生活(“ live ”分支)
  • 1 remote bare repo ( in order to be able to push to it , pushing either the staging or the live branch 1个远程裸仓( 为了能够推送它 ,推动staginglive分支
  • 1 post-update hook for the bare repo in order to checkout and update a working tree (representing your actual "staging" or "live" files) 为了检查并更新工作树 (代表您的实际“暂存”或“实时”文件),为裸仓库提供1 个更新后挂钩
  • 1 local repo where you add the bare repo as remote, and where you can push to staging or to live. 1个本地仓库,您将裸仓库添加为远程仓库,以及可以推送到临时或生活的地方。
    You can also clone the bare repo to any other local computer you need to work on. 您还可以将裸仓库克隆到需要处理的任何其他本地计算机。

The difference between a post-receive and a post-update hook is that the post-update one is executed once for every branch modified: 一个之间的差post-receivepost-update钩是, post-update一个是为每改性分支执行一次:
See the " Git hook to update various web folders based on branch pushed to remote server " SO question. 请参阅“ Git hook以根据分支推送到远程服务器更新各种Web文件夹 ”SO问题。

On the initial push, do a " git push --all origin " and all branches will be created on the remote bare repo. 在初始推送时,执行“ git push --all origin ”,并在远程裸仓库上创建所有分支。

The idea is no pulling should be involved on the server side: Only a git --work-tree=/path/to/your/live/files/ checkout live or git --work-tree=/path/to/your/staging/files/ checkout staging , depending on the parameters of the post-update hook: you only checkout the files of the bare repo into these 'folders' on the server. 这个想法是不应该在服务器端涉及:只有git --work-tree=/path/to/your/live/files/ checkout livegit --work-tree=/path/to/your/staging/files/ checkout staging ,具体取决于更新后挂钩的参数:您只需将裸仓库的文件签出到服务器上的这些“文件夹”中。

If you do a ruby script for your hook, make sure to: 如果你为钩子做了一个ruby脚本,请确保:

  • use the right shebang : #!/usr/bin/env ruby , 使用正确的shebang#!/usr/bin/env ruby
  • surround your git command with backtick should be enough: `git ...` , like in this script , 使用反引号包围你的git命令就足够了: `git ...`就像在这个脚本中一样
  • use ENV['HOME'] to specify the homedir of the current user within said script, if you want commands like `cd ~/stagedomain.com` or `--work-tree=~/stagedomain.com` to work (with `~` being set to the right path), 使用ENV['HOME']指定所述脚本中当前用户的homedir,如果你想要`cd ~/stagedomain.com``--work-tree=~/stagedomain.com`可以工作(带有`~`设置在正确的路径上),
  • if you chose to git pull , unset GIT_DIR on the same line than the other commands like in your other question : `cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage` . 如果你选择git pull ,在同一行上取消GIT_DIR ,而不是在你的另一个问题中 `cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage`其他命令: `cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage`

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

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