Skip to content

Instantly share code, notes, and snippets.

@hupili
Last active December 30, 2015 03:09
Show Gist options
  • Save hupili/7767420 to your computer and use it in GitHub Desktop.
Save hupili/7767420 to your computer and use it in GitHub Desktop.
Use Git to update your web server.

Use Git to update your web server.

Many hosting service now supports not only FTP and web uploading but also SSH. It's more convenient to use the Git flow:

  • Modify and test your site locally.
  • Push to remote Git repo, which triggers the update of your website.

My settings:

  • Suppose the HTTP server use ${HOME}/html as the root dir of your website.
  • git init the ${HOME}/html as a Git repo with working dir.
  • git init --bare the ${HOME}/html.git as a bare repo for push/pull.
  • Edit ${HOME}/html.git/hooks/post-update as follows.

Handle the nasty environments

If you have control of the server and installs Git via package manager, the above settings should be enough. In my case ( http://hupili.net/ ), the server is really nasty. You can not find evn the which command, let alone Git! After compiling Git, there are some further settings needed.

I put the Git paths in a .git-path file (you may also want to source it in .bashrc)

export GIT_BIN=${HOME}/git
export PATH=${GIT_BIN}/bin:${PATH}
export LD_LIBRARY_PATH=${GIT_BIN}/lib:${LD_LIBRARY_PATH}
export GIT_EXEC_PATH=${GIT_BIN}/libexec/git-core

and modify the .ssh/authorized_keys:

command="if [[ \"x${SSH_ORIGINAL_COMMAND}x\" == \"xx\" ]]; then /bin/bash; else source ~/.git-path; eval \"${SSH_ORIGINAL_COMMAND}\"; fi;" ssh-rsa XXXXX_YOUR_PUB_KEY_HERE YOUR_USER@YOUR_MACHINE_HERE
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
#exec git update-server-info
target=${HOME}/html
cd $target
unset GIT_DIR
git pull
@hupili
Copy link
Author

hupili commented May 26, 2014

rsync can come handy in cases where your HTML root is a subfolder of Git and the hosting server does not support soft linking well.

rsync -av --delete -r -L SRC/ DST

In my use case, SRC/ is the subdir in the Git repo. DST is the dir that the hosting server use for HTTP document root.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment