Deploy website via Git


On your server

~/www$ git init
Initialized empty Git repository in /home/

Enable current branch rewrite, cause with are using non-bare repo:

~/www$ git config receive.denyCurrentBranch ignore

Create hook .git/hooks/post-receive and write into it:

cd ..
git reset --hard

Make your hook executive: ~/www$ chmod +x .git/hooks/post-receive

On developer machine

Create repo, add all needed files to it, make your first commit.

git init
git config --local "Developer Name"
git config --local ""
git add <some_files>
git commit -m 'initial commit'

Now add remote repository as origin:

git remote add origin
git push --set-upstream origin master

How to work with repository?

After you have changed some files you should add them to commit and send this commit to repository.

git commit -am 'some commit message describes accepted changes'
git push


Make sure that your .htaccess file blocks access to .git/ and .gitignore. To prevent access put the following lines to your .htaccess:

# Prevents access to dot files (.git, .htaccess) - security.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]

What if I change files directly on server?

All uncommited file changes on server will be ignored after git push from developer machine. If you still want to change files directly on server you should use another two hooks:


cd ..
git stash save --quiet
git stash show || true


cd ..
git reset --hard
git stash pop --quiet || git reset --hard