{"id":790,"date":"2013-10-01T07:06:49","date_gmt":"2013-10-01T07:06:49","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=790"},"modified":"2013-10-01T07:06:49","modified_gmt":"2013-10-01T07:06:49","slug":"control-git-with-php","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/control-git-with-php\/","title":{"rendered":"Control Git with PHP"},"content":{"rendered":"

\"gitphp\"<\/a>
\nGit is getting popular day by day and there are many web front-ends for git available on the internet. In this article, i will give you an alternative option to work with git directly from PHP, i think it can be handy in some cases.<\/p>\n

To start working with it, we will need to find out apache web user on the system. Normally, it’s www-data on linux and _www on Mac then we need to switch to that user using this command<\/p>\n

<\/p>\n

\nsu www-data; # or _www<\/pre>\n<\/pre>\n

then we create .ssh directory in www-data user home to generate & store ssh keys<\/p>\n

\n# make new .ssh home for www user\nmdkir \/var\/www\/.ssh;\n# change permission of .ssh directory\nchmod -Rf 777 \/var\/www\/.ssh;\n# generate new key\nssh-keygen -t rsa;\n<\/pre>\n

you will also need to allow www-data user read\/write permission, i have an article for it here<\/a>, but for short, you can use these command<\/p>\n

\n# change group ownershipng to www-data user, user _www for MacOSX\nsudo chown -R :www-data [path_to_your_source_code_directory]\nsudo chmod -R g+w [path_to_your_source_code_directory]\n<\/pre>\n

As a git user, you know that git requires your ssh public key to be added to git so you can pull\/push code. in this case, we need to add www-data user ssh key to git repository. please do it like you always do. After having www-data user ssh key added, you can execute following command to do initial git pull to accept git server.<\/p>\n

\n# add \/var\/www\/.ssh\/id_rsa.pub to your git deploy key then execute these line\ncd GIT_REPO_FOLDER;\ngit pull origin; # it will ask you to accept, choose yes.\nexit; # return to your user\n<\/pre>\n

finally, create php script like this (note : this is just an example for magento devsite update)
\n deploygit.php<\/strong><\/p>\n

\n<?php\necho "\\n1.switch branch\\n";\necho(exec('git checkout devsite;'));\n\necho "\\n\\n2.update branch\\n";\necho(shell_exec('git pull origin devsite 2>&1'));\n\necho "\\n\\n3.clean cache\\n";\necho(exec('rm -rfv var\/cache\/*;'));\n\necho("\\n=======");\ndie("\\ndone!");\n?>\n<\/pre>\n

place it at root of your website source code, execute [you_web_site_base_url]\/deploygit.php to run script<\/p>\n","protected":false},"excerpt":{"rendered":"

Git is getting popular day by day and there are many web front-ends for git available on the internet. In this article, i will give you an alternative option to work with git directly from PHP, i think it can be handy in some cases. To start working with it, we will need to find…<\/p>\n

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,60,66],"tags":[],"aioseo_notices":[],"views":6,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/790"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=790"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/790\/revisions"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}