0

I used below code :

    echo exec("git add . ");  //this is working  
echo exec("git commit -am 'first commit' "); //also working  
echo exec("git push origin master");  //NOT WORKING, also not showing any error . 

I chowned folder permissions from user to www-data . So, some git commands are working but

GIT PUSH ORIGIN MASTER

is not working from php exec . What is the solution ? Also, please tell me why PUSH in exec NOT showing any error or msg, how can i see those msgs . Also,if possible, please provide me any good links for more advanced use of git commands from php exec .

Update : I also tried this : I added post-commit hook by creating file .git/hooks/post-commit

I added this code to it :

git push origin master

But I didnt get any msg or error after commiting, it just commited but didnt do any push.

Thanks !

3
  • Did you try git push origin master normally via terminal? What was the result? BTW, which directory is it getting executed? Commented Jun 28, 2012 at 6:48
  • It doesn't answer your question, but have you looked at PHP bindings for libgit2? Commented Jun 28, 2012 at 7:13
  • No I didnt check PHP bindings . And Mr. Praveen , it is getting executed at /var/www/fd/index.php . I tried it using terminal and got permission denied (public key) error when using sudo . And without using sudo, it pushed but failed writing resullt to git as sudo is required to write data in that folder(as i chowned as www-data) Commented Jun 28, 2012 at 7:34

2 Answers 2

2

I assume the push command will try to push to a remote repository (i.e. not another folder on your system but a remote server behind SSH/HTTPS).

In this case you are most likely missing a HTTPS client certificate HTTPS or an SSH key. Your webserver (and therefore PHP) most likely runs as a different user and does not have access to the private key. Next to that if the private key needs a password it will not work because the exec command is not an interactive session.

Anyway I heavily recommend you to use some sort of bindings and not calling the binaries. Any direct call will start a new process and this is way more inefficient that calls into a library.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a good answer. It answered the commit and push. One needs the permission of the web server (apache) which on linux is 'www-data'. There will be a .ssh directory created in /var/www and it needs your id_rsa (or other keys) as read/write to www-data. Also the .git directory and its contents to be read/write to group www-data. Permissions can be a real conundrum.
0

Have you checked which branch your are on? 'git status' will tell you.

I'd also recommend working on a branch and not directly on master.

git branch dev_test

git checkout dev_test

git add .

git commit -a -m "first comment"

git push origin dev_test

git checkout master

git merge dev_test

git push origin master

1 Comment

# On branch master # Your branch is ahead of 'origin/master' by 5 commits. # nothing to commit (working directory clean)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.