Git 推送到实时服务器

发布于 2024-09-19 22:30:11 字数 758 浏览 9 评论 0原文

我们有一个网站,其所有 PHP/HTML/JS/CSS/etc 文件都存储在 Git 存储库中。

目前,我们的存储库有 3 种类型的计算机(或用例)。

  • 本地开发人员:拉取最新更改,进行更改,提交到本地存储库,推送到主服务器
  • 主服务器:中央存储库,所有更改都推送到主服务器
  • Web服务器:部署网站时从主服务器拉取更改

所以目前我们:

local: git push origin master
local: password: ********
local: ssh [email protected]
webserver: password: ********
webserver: cd ~/domain.example/
webserver: git pull origin master

所以我的问题是:有没有一种方法可以从我的本地计算机直接推送到网络服务器?

IE。

local: git push origin master
local: password: ********
local: git push webserver master
local: password: ********

We have a website that has all its PHP/HTML/JS/CSS/etc files stored in a Git repository.

We currently have 3 types of computers (or use cases) for the repository.

  • 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

So currently we:

local: git push origin master
local: password: ********
local: ssh [email protected]
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?

ie.

local: git push origin master
local: password: ********
local: git push webserver master
local: password: ********

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

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

发布评论

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

评论(6

新人笑 2024-09-26 22:30:11

是的,您可以直接推送到您的网络服务器,但我不推荐它,因为您应该只推送到使用 --bare 参数克隆的存储库。我会利用 Git 挂钩系统让主存储库自动更新 Web 服务器上的存储库。查看更新后挂钩:

http://git-scm。 com/docs/githooks

这个脚本可以依次通过 SSH 登录到 Web 服务器并执行

cd ~/example.com/
git checkout master
git pull origin master

此操作,这样您只需要专注于推送到中央服务器而不必关心 Web 服务器,它会推送后始终会更新。如果你可以自动化某些东西,那就自动化它:)

我什至为你找到了一篇关于在脚本中通过 SSH 登录的好文章(如果你必须使用密码,如果已经设置了 ssh 密钥,这很简单):

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

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-update hook in:

http://git-scm.com/docs/githooks

This script could in turn login to the web server via SSH and do

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. 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):

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

看轻我的陪伴 2024-09-26 22:30:11

我有同样的查询,并且对这里当前投票最高的答案不满意,最终遵循 git-website -howto 很好地概述了该过程,并且在我看来是一种更干净、更快捷的方法。

TL;DR,git init --bare 在您的 Web 服务器上创建一个新的存储库,您将在其中从您的开发计算机推送更改。当 Web 存储库收到您的更改时,它会触发 post-receive 挂钩,然后将文件复制到您的 Web 根目录。

我喜欢这种方法,因为接收后挂钩在您的服务器上完成工作,因此您的本地计算机可以更快地推送并释放自身。这也使得为特定分支设置远程跟踪变得非常容易。因此,您可以有一个名为 product 的分支来更新您的 Web 服务器,而您的 master 继续用于开发并链接到其他地方的 git 存储库。

注意:您需要在 Web 服务器存储库上运行 git config receive.denycurrentbranchignore 来在推送时抑制本地开发盒上的警告。

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.

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.

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.

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.

远昼 2024-09-26 22:30:11

查看 http://www.kernel.org/pub/software/scm/git/docs/v1.6.0.6/git-push.html

所以你会尝试:

git push ssh://[email protected]/~admin/domain.example/ master

添加:
我认为您所要求的部分内容是如何拥有多个远程存储库。

git remote add webserver ssh://[email protected]/~admin/domain.example/

这允许你运行:

   git push origin master
   git push webserver master

Look at the Git URLs portion of http://www.kernel.org/pub/software/scm/git/docs/v1.6.0.6/git-push.html

so you would try:

git push ssh://[email protected]/~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://[email protected]/~admin/domain.example/

that allows you to run:

   git push origin master
   git push webserver master
梦在夏天 2024-09-26 22:30:11

我认为您正在寻找的功能如下所述: http://debuggable.com/posts/git-tip-auto-update-working-tree-via-post-receive-hook:49551efe-6414-4e86- aec6-544f4834cda3

local 您可以将网络服务器添加为远程服务器,就像您执行其他操作一样:

git remote add webserver admin@webserver:/path/to/repo.git/
# push only master branch by default
git config remote.webserver.push master  

现在,当您准备好推送时,您可以执行以下操作:

git push webserver

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

From local you can add the webserver as a remote, just like you would do any other:

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
瀞厅☆埖开 2024-09-26 22:30:11

在部署本地更改之前,检查目标服务器上是否有任何更改。

添加到部署脚本以确保服务器上没有任何更改:

$ git ls-files -dmo --exclude-standard

如果有未更改的文件将为空,比解析 git status 更容易

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

老街孤人 2024-09-26 22:30:11
  1. 添加远程 $ git Remote add server ssh://server_hostname:/path/to/git/repo
  2. 在服务器上签出临时分支 $ git checkout -b temp
  3. 推送更改 $ git Push server
  4. 签出上一个分支并删除临时分支
    <代码>
    $ git checkout - # 上一个分支的简写,git checkout @{-1}
    $ git 分支 -d 温度

我在这里有更多背景信息: https:// medium.com/@2upmedia/git-push-to-live-server-5100406a26

  1. Add remote $ git remote add server ssh://server_hostname:/path/to/git/repo
  2. Checkout temp branch on server $ git checkout -b temp
  3. Push changes $ 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

I have more background information here: https://medium.com/@2upmedia/git-push-to-live-server-5100406a26

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文