Skip to content

Instantly share code, notes, and snippets.

@lazypower
Created October 10, 2012 15:36
Show Gist options
  • Save lazypower/3866399 to your computer and use it in GitHub Desktop.
Save lazypower/3866399 to your computer and use it in GitHub Desktop.
Deploy your website using git hooks instead of ssh

server-side:

  • navigate to your user folder /home/username/
  • create a folder for your repo. 'repo'
  • create a folder inside of your repo. call it whatever. 'test'
  • cd into folder.
  • git init --bare

local:

  • navigate to local repo

  • test ssh connection to the server ex. "ssh [email protected] 'echo it worked'"

  • if it fails:

  • navigate to project root .ssh folder

  • copy contents of id_rsa.pub file

  • on server, navigate to git .ssh folder

  • paste local ssh key into "authorized_keys" folder

  • on local, try the ssh command again, should work now.

-add ssh address to remotes for folder where you initialized the bare repository, call it staging/production or whatever ex. "git remote add staging ssh://[email protected]/home/username/repo/test"

  • add files to local commit. just make a random file if it's empty or change an existing one if there's already stuff there that hasn't changed.

  • commit

  • run command below replace staging with whatever you called your remote

  • git push staging +master:refs/heads/master -- this only applies the first time. Afterwords you can just push staging master.

server-side:

  • navigate to the folder that holds the bare repo. 'test'
  • navigate into "hooks" folder
  • create a new file called post-receive with the following contents where GIT_WORK_TREE url points to the folder for your repo. 'repo' :
#!/bin/sh
GIT_WORK_TREE=/home/username/repo
export GIT_WORK_TREE
git checkout -f
  • save that file as post-receive
  • make file executable with the following command... "chmod +x post-receive"

local:

  • make a change to one of the files in your repo, commit file, then push to your remote server 'staging'
  • changes should now appear automagically in your remote repo as well :D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment