git网站更新策略 - 如何同步开发和实时存储库? [英] git website update strategy - how to sync dev and live repositories?

查看:118
本文介绍了git网站更新策略 - 如何同步开发和实时存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在构建我的git-powered-website更新和备份策略的方式:

我有通过SSH访问托管网站的Linux VPS。这是我做的:
$ b $ 1)在网站服务器 - 创建一个git仓库,在正确的网站文件夹(公共根之前一级):



$ p $ cd $ path $ $ b $ g $ g $ b $ git add -A
git commit -m今天的网站。

2)在备份服务器上 - 为了备份目的,在另一个VPS上创建镜像回购:

  git clone --mirror ssh://user@example.com/path/to/website website_backup 
code>

请注意,镜像回购也是一个裸仓库(没有检出工作树)。
$ 3)设置CRONJOBS - 一个在网站服务器上,以吸收更好的文件系统更改(可以通过脚本,FTP等进行更改)。它每天运行以下bash脚本:

 #!/ bin / bash 
date = $(date +% d /%m /%Y)
cd / path / to / website
git add -A -v
git commit -m在$ {date}网站上完成的更改
exit 0

通过这种方式,实时网站更改被提交到存储库主分支。 / p>

另一个cronjob在备份服务器上设置。它每天运行以下脚本,紧接着上面的脚本:

 #!/ bin / bash 
cd / path / to / website_backup
git fetch -u ssh://user@example.com/path/to/website
exit 0

通过这种方式,我在备份服务器上每天更新一次备份,这也是一个git repo,允许我在必要时及时向后移动。我不需要担心因意外覆盖或删除而丢失东西......并且这个过程是自动的!



我每天收到几封电子邮件从cronjobs。它允许我检查网站中发生了什么变化,并确认两个cronjob运行正常。 (另一个cronjob设置为执行数据库备份。)

4)设置开发(本地重复工作树) - 我直接从网站上检出一个副本,然后创建一个名为dev的新本地分支:

  git clone ssh://user@example.com/path / to / website website_local 
git checkout -b dev

现在,我可以玩开发分支,并做我的工作。



从这一点,我想知道:


  • 如何将更改返回到实时网站?

  • 如何从网站获取更改并合并到我的开发分支中?



简而言之:如何正确地同步现场网站与开发分支而不会搞砸事情?

解决方案

以下是我为满足将我的开发工作推向生产(实时网站)的需求而实现的解决方案,并使本地存储库与ch保持同步在现场网站上发生的天灾......



更新网站只是推动我的本地开发分支到网站存储库......

  git push origin dev 



<然后,将更改合并到实时网站树中。我需要SSH登录到网站服务器,并在网站文件夹中运行以下命令:

  git merge dev 

这会在dev分支处将推送的更改带到主分支(这是



*改进更新过程*



自动运行合并,而不需要从服务器命令行登录并运行合并命令,我在实时网站存储库中添加了post-receive钩子。首先,我创建了钩子文件,将其设置为可执行文件,然后编辑文件:

  touch / path / to / website / .git / hooks / post-receive 
chmod a + x /path/to/website/.git/hooks/post-receive
pico /path/to/website/.git/hooks/post-收到

我的post-receive钩子文件的内容是:

 #!/ bin / sh 
unset GIT_DIR
cd / path / to / website
echo将更改合并到主分支。
git merge --ff-only dev
exit 0

请注意 - 仅限ff-only 选项添加到合并命令。为什么在那里?这是因为,作为一个自动化过程,我不想将存储在我的实时网站文件中的冲突合并。所以,使用这个选项,我强制合并发生只有当我有一个干净的快速前进上下文。如果这种干净的合并不可能发生,那么我可以登录到服务器,然后手动解决案例,或者使用其他方法解决问题。



*避免冲突和同步*



为避免服务器发生合并冲突,即确保成功实现快速合并,这是一个好主意使用来自远程回购的最新更改更新本地回购。换句话说:在推送我们的更改之前,使用最新的实时网站更改(远程主分支)更新本地开发分支。这可以这样做:

  git pull origin master 

更好的是:首先更新本地主分支,然后将其合并到本地开发分支(听起来像是一个rebase):

  git stash保存
git checkout master
git pull origin master
git checkout dev
git stash pop
git merge master

通过这种方式,我们的本地主分支与远程实时网站主服务器保持同步分支,合并在本地执行100%。



*返回简单*

我已经创建了一个别名来促成一些事情:

  git config alias.deploy'!git stash save&& git checkout master&& git pull origin master&& git checkout dev&& git stash pop; git merge master&& git push origin dev'

现在,我可以使用deploy别名执行实时站点更新,如下所示:

  git deploy 

它会:


  1. 切换到本地主分支

  2. 使用网站最新提交的更改(同步)更新本地主分支

  3. 切换回开发分支

  4. 将更改合并到本地开发分支如果需要,可以在这里解决冲突问题)

  5. 将本地dev分支推送到远程网站dev分支

  6. 正确设置post-receive钩子服务器,它会自动快速转发网站回购,所以开发人员的更改将发布到生产中!






我有这个设置工作,它满足了我目前的需求,这很简单。


Here is how I have been constructing my git-powered-website update and backup strategy:

I have SSH access to the Linux VPS where the website is hosted. Here is what I did:

1) AT THE WEBSITE SERVER - Created a git repo, at the proper website folder (one level before public root):

cd /path/to/website
git init
git add -A
git commit -m "Website as of today."

2) AT THE BACKUP SERVER - Created a mirror repo, for backup purposes, at another VPS:

git clone --mirror ssh://user@example.com/path/to/website website_backup

Note that a mirror repo is also a bare repository (no checked out working tree).

3) SET UP CRONJOBS - One at the website server, to absorb wesbite file system changes (changes can be done by the scripts, by FTP, etc). It runs the following bash script, daily:

#!/bin/bash
date=$(date +%d/%m/%Y)
cd /path/to/website
git add -A -v
git commit -m "Changes done at to website at ${date}"
exit 0

This way, the live website changes are committed to the repository master branch.

Another cronjob is set up at the backup server. It runs the following script daily, right after the other one above:

#!/bin/bash
cd /path/to/website_backup
git fetch -u ssh://user@example.com/path/to/website
exit 0

This way I have at the backup server a daily updated "backup", which is also a git repo, allowing me to move backwards in time if necessary. I don't need to be much afraid of losing stuff by accidental overwrites or deletions... and the process is automated!

I receive daily a couple of e-mails from the cronjobs. It allows me to check what has been changed in the website, and to acknowledge that both cronjobs are running correctly. (Another cronjob is set up to perform the database backup.)

4) SET UP DEVELOPMENT (LOCAL REPO + WORKING TREE) - I checked out a copy directly from the website, and then created a new local branch called "dev":

git clone ssh://user@example.com/path/to/website website_local
git checkout -b dev

Now, I can play with the development branch, and do my work.

From this point, I'd like to know:

  • How to push my changes back to the live website?
  • How to get changes from the website back and merged to my development branch?

In short: how to properly sync live site with dev branch without messing things up?

解决方案

Here is the solution I achieved to satisfy the needs of pushing my development work to "production" (live website) and also keep my local repository up to date with the changes occurring at the live website...

To update the website is simply a matter of pushing my local development branch to the website repository...

git push origin dev

...and then, merge the changes into the live website tree. I need to SSH log in to the website server, and run the following command at the website folder:

git merge dev

This will bring the pushed changes, at "dev" branch, to the "master" branch (which is the live site current branch).

* IMPROVING THE UPDATE PROCESS *

To automatically run the merge, without needing to login and run the merge command from the server command line, I added a post-receive hook to the live website repository. First, I created the hook file, made it executable, and then edited the file:

touch /path/to/website/.git/hooks/post-receive
chmod a+x /path/to/website/.git/hooks/post-receive
pico /path/to/website/.git/hooks/post-receive

The contents of my post-receive hook file are:

#!/bin/sh
unset GIT_DIR
cd /path/to/website
echo "Merging dev changes to master branch."
git merge --ff-only dev
exit 0

Note the --ff-only option added to the merge command. Why is it there? It is there because, being an automated process, I don't want to have merge conflicts stored into my live website files. So, using this option, I enforce the merge to happen only if I have a clean fast-forward context. If this clean merge can't happen, then I may log in to the server, and manually resolve the case, or solve the problem using another approach.

* AVOIDING CONFLICTS AND SYNCHRONIZING *

To avoid merge conflicts at the server, i.e., to ensure a successfull fast-forward merge there, it is a good idea to update the local repo with the latest changes from the remote repo. In other words: update the local development branch with latest live website changes (remote master branch), prior to pushing our changes. This could be done like this:

git pull origin master

Better yet: let's first update the local master branch, and then merge it into the local development branch (sounds like a rebase):

git stash save
git checkout master
git pull origin master
git checkout dev
git stash pop
git merge master

This way, our local master branch is kept in sync with the remote live website master branch, and the merge is performed 100% locally.

* BACK TO SIMPLICITY *

I have created an alias to facilitate things:

git config alias.deploy '!git stash save && git checkout master && git pull origin master && git checkout dev && git stash pop ; git merge master && git push origin dev'

Now, I can perform the live site update by using the "deploy" alias, like this:

git deploy

It will:

  1. Switch to local master branch
  2. Update the local master branch with the website latest committed changes (sync)
  3. Switch back to dev branch
  4. Merge the changes to the local dev branch (conflict resolution here if needed)
  5. Push the local dev branch to the remote website dev branch
  6. Having the post-receive hook properly set up at the server, it will automatically fast-forward the website repo, so the dev changes will be published to production!


I have this setup working, and it is satisfying my current needs, which are simple.

这篇关于git网站更新策略 - 如何同步开发和实时存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆