Skip to content

Instantly share code, notes, and snippets.

@maxkueng
Created May 15, 2014 00:38
Show Gist options
  • Save maxkueng/646f843a2e58d683991f to your computer and use it in GitHub Desktop.
Save maxkueng/646f843a2e58d683991f to your computer and use it in GitHub Desktop.

Mini tutorial for /u/smug_rum_smuggler and /u/itmcb.

Thread ->

Beware, I haven't tested this. Let's assume a few things:

  • your project is called "myproject"
  • your server is at "myserver.com"
  • you have SSH access to the server with the user "bob"
  • your project on the server is at /home/bob/myproject
  • the "master" branch is the stable branch

On the server

Set up a directory where you put all your git repos:

mkdir ~/gitrepos

Create a bare Git repository as the source for your code directory where you put all your git repos:

cd ~/gitrepos
git init --bare myproject.git

Go to your project directory and initialize a new repo (unless it's already a checkout) and add the bare repo as a remote called "source":

cd ~/myproject
git init
git remote add source /home/bob/gitrepos/myproject.git

Set up the post-receive hook. Edit the file ~/gitrepos/myproject.git/hooks/post-receive and add the following:

#!/bin/sh

cd /home/myproject
env -e git pull --rebase source master

# add more commands below, like e.g. `npm install` if node project

Make the file executable:

chmod +x ~/gitrepos/myproject.git/hooks/post-receive

On your workstation

Go to your local repo and add a remote called "deploy":

cd path/to/my/repo/
git remote add deploy ssh://[email protected]:/home/bob/gitrepos/myproject.git

Make some changes in the master branch and commit:

git checkout master
echo hello > testfile
git add testfile
git commit -m ohlala

Push the master branch to the remote repo:

git push deploy master

On the server

Verify that your project has the updated code:

ls -l ~/myproject 
# should have the updated code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment