简体   繁体   English

git 网站更新攻略 - 如何同步开发和实时存储库?

[英]git website update strategy - how to sync dev and live repositories?

Here is how I have been constructing my git-powered-website update and backup strategy:以下是我构建 git-powered-website 更新和备份策略的方式:

I have SSH access to the Linux VPS where the website is hosted.我有 SSH 访问托管网站的 Linux VPS。 Here is what I did:这是我所做的:

1) AT THE WEBSITE SERVER - Created a git repo, at the proper website folder (one level before public root): 1)在网站服务器 - 在适当的网站文件夹(公共根之前的一级)创建了 git 存储库:

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: 2) 在备份服务器上 - 在另一个 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). 3) 设置 CRONJOBS - 一个在网站服务器上,用于吸收 wesbite 文件系统更改(更改可以通过脚本完成,通过 FTP 等)。 It runs the following bash script, daily:它每天运行以下 bash 脚本:

#!/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.在备份服务器上设置了另一个 cronjob。 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.这样我在备份服务器上有一个每日更新的“备份”,这也是一个 git 存储库,如果需要,我可以及时向后移动。 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.我每天都会收到来自 cronjobs 的几封电子邮件。 It allows me to check what has been changed in the website, and to acknowledge that both cronjobs are running correctly.它允许我检查网站中的更改,并确认两个 cronjobs 都在正确运行。 (Another cronjob is set up to perform the database backup.) (设置了另一个 cronjob 来执行数据库备份。)

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": 4)设置开发(本地回购+工作树) - 我直接从网站上签出一个副本,然后创建一个名为“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:我需要 SSH 登录网站服务器,在网站文件夹下运行以下命令:

git merge dev

This will bring the pushed changes, at "dev" branch, to the "master" branch (which is the live site current branch).这会将“dev”分支中的推送更改带到“master”分支(即实时站点当前分支)。

* 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.为了自动运行合并,无需登录并从服务器命令行运行合并命令,我在实时网站存储库中添加了一个 post-receive 挂钩。 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.请注意添加到合并命令的--ff-only选项。 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, ie, 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.这样,我们的本地 master 分支与远程直播网站 master 分支保持同步,并且 100% 本地执行合并。

* 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:现在,我可以使用“deploy”别名执行实时站点更新,如下所示:

git deploy

It will:它会:

  1. Switch to local master branch切换到本地 master 分支
  2. Update the local master branch with the website latest committed changes (sync)使用网站最新提交的更改更新本地主分支(同步)
  3. Switch back to dev branch切换回 dev 分支
  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将本地 dev 分支推送到远程网站 dev 分支
  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!在服务器上正确设置 post-receive 钩子后,它将自动快进网站 repo,因此开发更改将发布到生产中!

I have this setup working, and it is satisfying my current needs, which are simple.我有这个设置工作,它满足了我当前的需求,这很简单。

You might want to look at http://joemaller.com/990/a-web-focused-git-workflow/ and http://toroid.org/ams/git-website-howto for further information on integrating git and web deployment systems. You might want to look at http://joemaller.com/990/a-web-focused-git-workflow/ and http://toroid.org/ams/git-website-howto for further information on integrating git and web部署系统。

Remember, git is not a web deployment system (though with some simple scripts it can work that way for people with simple needs).请记住, git 不是 web 部署系统(尽管使用一些简单的脚本,它可以为有简单需求的人工作)。

Or, you could just use Git and Jekyll as Github does或者,您可以像Github那样使用 Git 和 Jekyll

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

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