Skip to content

Instantly share code, notes, and snippets.

@tyom
Created January 23, 2012 09:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyom/1662259 to your computer and use it in GitHub Desktop.
Save tyom/1662259 to your computer and use it in GitHub Desktop.
Managing remote repository on a server and receive hooks
First, create a new local git repo:
$ mkdir my-project && cd my-project
$ git init
Initialized empty Git repository in ~/projects/my-project/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "Initial commit"
Then, on the remote server create a new directory for the new repo we just created:
$ cd ~/git/
$ mkdir my-project.git && cd my-project.git
$ git init --bare
Initialized empty Git repository in ~/git/my-project.git/
Add hooks to publish the website in a separate directory:
$ mkdir /var/www/my-website.org
$ vim ~/git/my-project/.git/hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/my-website.org git checkout -f
$ chmod +x hooks/post-receive
Back to local repo, add remote branch:
$ git remote add production username@my-website.org:/home/me/git/my-project.git
$ git push production +master:refs/heads/master
We're all set. To publish website:
$ git push production
Additionally we can publish to more than one remote server. In .git/config add:
[remote "production"]
url = ssh://my-website.org/home/me/my-project.git
url = ssh://mirror.another-server.org/home/me/my-project.git
Bonus publish branches on remote server (git push):
#!/bin/bash
#
publish_path="/var/www/proto/multiselect"
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [[ "master" == "$branch" ]]; then
if [ ! -d "$publish_path/current" ]; then
mkdir "$publish_path/current"
fi
git --work-tree=$publish_path/current checkout -f $branch
echo 'Changes pushed to current.'
else
if [ ! -d "$publish_path/dev" ]; then
mkdir "$publish_path/dev"
fi
if [ ! -d "$publish_path/dev/$branch" ]; then
mkdir "$publish_path/dev/$branch"
fi
git --work-tree=$publish_path/dev/$branch checkout -f $branch
echo 'Changes pushed to dev.'
fi
done
Ref: http://blog.ekynoxe.com/2011/10/22/git-post-receive-for-multiple-remote-branches-and-work-trees/
First, create a new local git repo:
$ mkdir my-project && cd my-project
$ git init
Initialized empty Git repository in ~/projects/my-project/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "Initial commit"
Then, on the remote server create a new directory for the new repo we just created:
$ cd ~/git/
$ mkdir my-project.git && cd my-project.git
$ git init --bare
Initialized empty Git repository in ~/git/my-project.git/
Add hooks to publish the website in a separate directory:
$ mkdir /var/www/my-website.org
$ vim ~/git/my-project/.git/hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/my-website.org git checkout -f
$ chmod +x hooks/post-receive
Back to local repo, add remote branch:
$ git remote add production ssh://my-website.org/home/me/git/my-project.git
$ git push production +master:refs/heads/master
We're all set. To publish website:
$ git push production
Additionally we can publish to more than one remote server. In .git/config add:
[remote "production"]
url = ssh://my-website.org/home/me/my-project.git
url = ssh://mirror.another-server.org/home/me/my-project.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment