Last active
          October 13, 2025 02:29 
        
      - 
            
      
        
      
    Star
      
          
          (150)
      
  
You must be signed in to star a gist  - 
              
      
        
      
    Fork
      
          
          (52)
      
  
You must be signed in to fork a gist  
- 
      
 - 
        
Save lemiorhan/8912188 to your computer and use it in GitHub Desktop.  
    Post-receive hook to deploy the code being pushed to production branch to a specific folder
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| target_branch="production" | |
| working_tree="PATH_TO_DEPLOY" | |
| while read oldrev newrev refname | |
| do | |
| 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 " | DEPLOYMENT COMPLETED" | |
| echo " | Target branch: $target_branch" | |
| echo " | Target folder: $working_tree" | |
| echo " | Tag name : release_$NOW" | |
| echo " \==============================" | |
| fi | |
| done | 
This is great, thanks
cool!
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>
Lovely, I event added second if condition to push my development branch to different directory 💃
Unfortunatelly it doesn't work on Bitbucket =/
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
  
            
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 http://git-scm.com/book/en/Customizing-Git-Git-Hooks. The following procedure is useful to deploy static files, and scripts.
Local:
Add a new remote to your local repository
Create production branch
Do your development in development branch and merge into production to deploy to server
Remote:
Add the post-receive hook decribed in this gist. Do not forget to replacethe path to deploy with the
PATH_TO_DEPLOYplaceholder.Local:
Simply push you commits to the newly added remote
When you push the code to production branch of remote:
release_$releaseDateTimeon remote bare repo.