Skip to content

Instantly share code, notes, and snippets.

@digitup
Created August 27, 2012 19:36
Show Gist options
  • Save digitup/3491632 to your computer and use it in GitHub Desktop.
Save digitup/3491632 to your computer and use it in GitHub Desktop.
Git: website workflow 2
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:
Switch to local master branch
Update the local master branch with the website latest committed changes (sync)
Switch back to dev branch
Merge the changes to the local dev branch (conflict resolution here if needed)
Push the local dev branch to the remote website dev branch
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment