Welcome to Abdul Malik Ikhsan's Blog

Practical GIT (2) : Update remote file after push with configure post-receive hooks

Posted in GIT, Teknologi by samsonasik on November 20, 2012

GIT version control got one feature that I really like, named hooks. With hooks, we can configure what GIT do after metadata pushed to the remote server. It can ease our development because we don’t need to upload file manually, just say : git push remoteserver, so the file in our online web will be replaced.


The configuration is like this :
1. register remoteserver in your clone(local) app.

git remote add remoteserver ssh://you@yourserver/path/to/your/repo.git

2. Register receivepack and uploadpack file of server. open .git/config file ( in clone ( local )) app

[remote "remoteserver"]
        url = ssh://you@yourserver/path/to/your/repo.git
        fetch = +refs/heads/*:refs/remotes/remoteserver/*
        receivepack=/path/to/gitserverbin/git-receive-pack
        uploadpack=/path/to/gitserverbin/git-upload-pack

3. open you server /path/to/your/repo.git/hooks/post-receive file, if file not found, just create it. Configure like the following :

#!/bin/sh
#             replace everything in onlineweb folder after data pushed
GIT_WORK_TREE=/var/www/onlineweb git checkout -f

As Agung Susanto’s comment, you need to set the /path/to/your/repo.git/hooks/post-receive executable:

chmod +x /path/to/your/repo.git/hooks/post-receive

4. Ok, Let’s try

Abduls-MacBook-Pro:testclone abdulmalikikhsan$ vim testfile.txt //create and edit file...
Abduls-MacBook-Pro:testclone abdulmalikikhsan$ git add testfile.txt  //add the file...
Abduls-MacBook-Pro:testclone abdulmalikikhsan$ git commit -m "testfile" -a  //commit change ...
[master bdf3ea6] testfile
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 testfile.txt
Abduls-MacBook-Pro:testclone abdulmalikikhsan$ git push remoteserver  //push to the server....
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To ssh://you@yourserver/path/to/your/repo.git
   b3aa92a..bdf3ea6  master -> master

5. so the list of files in folder onlineweb will be like this :

Tagged with: , ,

10 Responses

Subscribe to comments with RSS.

  1. Joseph Crawford said, on December 4, 2012 at 3:10 am

    Here’s to hoping this is not being used to update a production website and only a development environment. Using this for updating production would violate the reasons for using version control with a build system like Travis, Jenkins, etc. Using a process like this would allow you to deploy broken code to production.

    • samsonasik said, on December 4, 2012 at 5:03 am

      Yes, it should be in development environment. but it still can be used in production environment if we actually pretty sure the code uploaded was solid.

      • Joseph Crawford said, on December 4, 2012 at 5:57 am

        Let’s agree to disagree on the production statement. Pretty sure is not positive and you cannot be positive without testing your code 🙂

      • samsonasik said, on December 4, 2012 at 11:53 am

        1. Pretty sure = tested ( ex : you can test with phpunit for php, junit for java ).
        2. Pretty sure = ( your local environment identics with your online environment )
        2. not everything must be tested ( typo wording should not be tested)
        3. most bosses want a change of their online site as fast as possible.

  2. Hayal Nurzipta said, on June 10, 2013 at 3:38 pm

    brief, simple, and clear. verry nice 😀

  3. Agung Susanto said, on February 2, 2015 at 11:15 am

    don’t forget to -> $ chmod +x hooks/post-receive

  4. […] example, you have a git hook on post-receive which run composer […]

  5. muwafiq said, on April 22, 2019 at 10:08 pm

    thanks for this tutorial


Leave a reply to Joseph Crawford Cancel reply