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

发布于 2024-10-28 19:46:30 字数 1515 浏览 7 评论 0原文

如何在 Git 中设置远程目录,以便可以在本地将阶段分支推送到远程并查看临时服务器(例如 stage.example.com)上的实时更改?

我的想法(我放弃 SVN 的部分原因)是我可以(本地)维护 3 个不同的“主”分支,如下所示:

  • ma​​ster - 用于本地开发,工作目录
  • stage - 应该与登台服务器目录同步(远程)
  • live - 这应该是可公开访问的网站(远程)

我的想法(以及其他人声称是可能的)是我可以从本地计算机维护这些远程“站点”,而无需不断登录远程服务器 shell 并运行 svn update(在我当前的 svn 工作流程中,我需要一直这样做...... )或者当然在我的 Git 工作流程中在远程运行 git pull 。

如何设置远程目录,以便可以在本地将我的 stage 分支推送到 staging 远程服务器 并查看上的更改(例如) 立即stage.example.com

然后,一旦stage一切正常并经过测试,我就可以在本地pushlive远程进行我已经测试过的这些更改在舞台上访问实时网站。

这是否可以完成,或者我在这里得到了一些疯狂的想法,而这些想法根本不适合用 Git 完成?

如果这很重要,这里有一些关于我的本地和远程服务器的统计信息:

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

另外,到目前为止我已经不成功尝试了以下工作流程:

  1. 创建一个 --bare Git远程存储库(这样我就可以从任何地方访问它)
  2. 将此远程存储库克隆到本地目录并使用 Git Tower 应用程序来管理它
  3. master (HEAD) 本地工作
  4. scp -r--bare git repo 从远程服务器复制到我的远程活动域 stage.example.com
  5. 将远程添加到本地工作副本,然后尝试推送to origin/stage

显然这不起作用,但我不知道为什么或如何做得更好。

我有 SVN 背景,对 Git 很陌生,但看过很多教程(Peepcode 和 ThinkVitamin),但仍然不知道如何设置。

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?

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:

  • master - used for local development, working dir
  • 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.

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?

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.

Can this even be done or am I getting crazy ideas here that are simply not meant to be done with 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)
  2. Clone this remote repo to a local directory and the use Git Tower app to manage it
  3. Work locally in master (HEAD)
  4. scp -r copy the --bare git repo from the remote server into my remote live domain stage.example.com
  5. Add remote to local working copy and then try to push to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-11-04 19:46:30

使用 DVCS(“分布式”VCS,如 Git 或 Mercurial)实现的一个概念是它添加发布的概念(推/拉)到分支的概念。
CVCS(“集中式”VCS,如 SVN)只有分支(以及一个要推送到服务器上的中央存储库)。

在您的情况下,暂存或实时是发布步骤,即不同的 Git 存储库准备好接收您想要在暂存或实时环境中看到的修改。

这意味着:

  • 2 个分支用于跟踪属于暂存(“staging”分支)或实时(“live”分支)的内容
  • 1 个远程裸存储库(为了能够推送它,推送staginglive 分支
  • 1 裸仓库的更新后挂钩 为了签出和更新工作树(代表您实际的“暂存”或“实时”文件)
  • 1 个本地存储库,您可以将裸存储库添加为远程存储库,并且可以推送到其中上演或生活。
    您还可以将裸存储库克隆到您需要使用的任何其他本地计算机。

post-receivepost-update 挂钩之间的区别在于,post-update 挂钩针对每个修改的分支执行一次:< br>
请参阅“Git hook 根据推送到远程服务器的分支更新各种 Web 文件夹”SO 问题。

在初始推送时,执行“git push --all origin”,所有分支都将在远程裸存储库上创建。

这个想法是服务器端不应该涉及拉动:只有 git --work-tree=/path/to/your/live/files/ checkout live 或 git --work -tree=/path/to/your/staging/files/ checkout staging,取决于更新后挂钩的参数:您只需将裸存储库的文件检出到服务器上的这些“文件夹”中。

如果您为挂钩编写 ruby​​ 脚本,请确保:

  • 使用 right shebang#!/usr/bin/env ruby​​
  • 用反引号包围你的 git 命令就足够了:`git ...`就像在这个脚本中一样,
  • 使用 ENV[' HOME'] 在所述脚本中指定当前用户的主目录,如果您需要诸如 `cd ~/stagedomain.com``--work-tree= 之类的命令~/stagedomain.com` 工作(将 `~` 设置为正确的路径),
  • 如果您选择 git pull,请取消设置 GIT_DIR 与其他命令位于同一行就像您的其他问题`cd ~/stage.mydomain.com &&取消设置 GIT_DIR && git pull core stage`

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.
A CVCS ("Centralized" VCS, like SVN) has only branching (and one central repo to push to on a server).

In your case, staging or live are publication steps, i.e. different Git repo ready to receive the modifications you want to see in staging or in live environment.

That would mean:

  • 2 branches to keep track of what belong to staging ("staging" branch) or live ("live" branch)
  • 1 remote bare repo (in order to be able to push to it, pushing either the staging or the live branch
  • 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 local repo where you add the bare repo as remote, and where you can push to staging or to live.
    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:
See the "Git hook to update various web folders based on branch pushed to remote server" SO question.

On the initial push, do a "git push --all origin" and all branches will be created on the remote bare repo.

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.

If you do a ruby script for your hook, make sure to:

  • use the right shebang: #!/usr/bin/env ruby,
  • surround your git command with backtick should be enough: `git ...`, like in this script,
  • 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),
  • 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`.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文