Skip to content

Instantly share code, notes, and snippets.

Last active February 8, 2023 10:06
What would you like to do?
Post-receive hook to deploy the code being pushed to production branch to a specific folder
while read oldrev newrev refname
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ -n "$branch" ] && [ "$target_branch" == "$branch" ]; then
GIT_WORK_TREE=$working_tree git checkout $target_branch -f
NOW=$(date +"%Y%m%d-%H%M")
git tag release_$NOW $target_branch
echo " /==============================="
echo " | Target branch: $target_branch"
echo " | Target folder: $working_tree"
echo " | Tag name : release_$NOW"
echo " \=============================="
Copy link

Deploying Code via Git Post-Receive Hook

The procedure is pretty simple. Whenever you merge your code from development branch into production branch, commit and then push to deploy_server (that is the server you want to deploy your code. It contains a remote git repository that we can push our code), the changes are deployed to the required folders in deploy_server by the post-receive hook. If you want more information about git hooks, please check The following procedure is useful to deploy static files, and scripts.


Add a new remote to your local repository

$ git remote add deploy_server REMOTE_URL

Create production branch

$ git branch production

Do your development in development branch and merge into production to deploy to server

(production) $ git merge development


Add the post-receive hook decribed in this gist. Do not forget to replacethe path to deploy with the PATH_TO_DEPLOY placeholder.


Simply push you commits to the newly added remote

(production) $ git push deploy_server production
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 223 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Already on 'production'
remote:    /===============================
remote:    | Target branch: production
remote:    | Target folder: /var/www
remote:    | Tag name     : release_20140211-1649
remote:    \==============================
   92f6e8e..f260343  production -> production

When you push the code to production branch of remote:

  • Post-receive hook puts your code (i.e. totally replaces the existing code via force checkout) to the directory you defined in the hook
  • Adds a new tag with a name release_$releaseDateTime on remote bare repo.

Copy link

silveur commented Jul 4, 2016

This is great, thanks

Copy link

ryanhs commented Feb 1, 2017


Copy link

A very useful bit of code and easy to follow, thanks.

It might be worth mentioning in your instructions that if you need to roll back to a previous commit you'll need to do something like git push <remote> +<commit>:<target_branch>

Copy link

Zielak commented Oct 3, 2018

Lovely, I event added second if condition to push my development branch to different directory 💃

Copy link

Unfortunatelly it doesn't work on Bitbucket =/

Copy link

I love the idea of this post-receive sample versus the standard push to master or main branch... Thanks I am trying it now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment