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
2. 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
3. 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
4. so the list of files in folder onlineweb will be like this :