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

主宰稳场 提交于 2019-11-30 10:58:56

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.

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.

Remember, git is not a web deployment system (though with some simple scripts it can work that way for people with simple needs).

Or, you could just use Git and Jekyll as Github does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!