3

I have a local git repository on my development machine.

I want to use git to push the code up to my production server. and trigger a restart of the web server once the code has been pushed there.

What are the commands to do this? I'm using debian.

/var/www/example.com is my local repository

Production hostname is production.example.com I have ssh + keys already setup.

chovy
  • 59,357
  • 43
  • 187
  • 234

2 Answers2

4

It is usually not a good idea to push code directly to a live website. For this you can create a bare repository on your server, outside the public directory. This repository will push changes to your Live website repository and will be like the gate keeper for your Website. To set things up, create a repository around your live website directory:

$ cd ~/www
$ git init
$ git add .
$ git commit -m"initial import of pre-existing web files"

With this created, move to a directory not accessible via HTTP. To keep things simple,lets call this bare directory HUB and the website directory LIVE. Initialize a bare repository here:

$ cd; mkdir site_hub.git; cd site_hub.git
$ git --bare init

Then, from inside your live website working directory, add this bare repository as a remote and push live website’s master branch.

$ cd ~/www
$ git remote add hub ~/site_hub.git
$ git remote show hub
* remote hub
  URL: /home/rizwan/site_hub.git
$ git push hub master

You need hooks to commit changes to the live repository. Create a post-update hook inside the HUB repository :

#!/bin/sh

echo
echo "**** Pulling changes into Live [Hub's post-update hook]"
echo

cd $HOME/www || exit
unset GIT_DIR
git pull hub master

exec git-update-server-info

Inside this hook, you can have the code to restart the server after the pull is completed.

Also, create a post-commit hook on the LIVE repo to send changes done to the live website back to HUB.

#!/bin/sh

echo
echo "**** pushing changes to Hub [Live's post-commit hook]"
echo

git push hub

On your local machine, add the HUB repository as a remote and push changes to it:

git remote add hub <hub-repository-url>

How this works is, you write some code and push it to the bare repository, which using its post-update hook to push changes to the live respository and restart the server.

automaticAllDramatic
  • 1,615
  • 1
  • 17
  • 25
2

I would recommend a post-receive hook on a bare repo on your server, with:

GIT_DIR=/path/to/bare/repo/.git 
GIT_WORK_TREE=/var/www/www.example.org git checkout -f

By pushing to a bare repo, you won't have any issue with a working tree out of sync with what you are pushing (since a bare repo has no working tree).
Then, you would checkout your bare repo directly on the live site directory structure (through the post-receive hook).

This is similar to "git GIT_WORK_TREE post-receive hook deployment remote".
Setting the GIT_DIR avoids the issue mentioned in "Git checkout in post-receive hook: “Not a git repository '.'".

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283