Practical GIT (2) : Update remote file after push with configure post-receive hooks
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 :
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.
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.
Let’s agree to disagree on the production statement. Pretty sure is not positive and you cannot be positive without testing your code 🙂
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.
brief, simple, and clear. verry nice 😀
Thanks 😉
don’t forget to -> $ chmod +x hooks/post-receive
thank you (y)
[…] example, you have a git hook on post-receive which run composer […]
thanks for this tutorial