简体   繁体   中英

shell_exec('git push origin master') won't work

I want to push a folder with data to an empty repository in atlassian Git stash. The repository is created via the rest api. The remote origin is set. When I enter

$ git push origin master

in the shell it works all fine. When I use the PHP interactive shell and enter

exec('git push origin master');

it also works. But when the code is called in my PHP file nothing happens. Of course the code is the same. I am in the right working directory (I change it with chdir() before the shell_exec ). No errors are reported (even when error_reporting is set to E_ALL ) and when i set the output argument in the exec() I get an empty array.

Can anyone help?

When you execute code interactively, or commands via the shell, you're executing under your user account and your shell environment.

I would assume that the reason this isn't working is that you're executing the PHP script as another user (and possibly lacking your ssh/git environment config).

More information is needed to better answer your question, but you may want to start by verifying the user you're executing your PHP file as is also able to perform a git push origin master by doing something like:

sudo su -s /bin/bash -c "cd /my/git/repo/dir; git push origin master" MYPHPEXECUTEUSER

I found out that i have to add '2>&1' to the end of a shell command to get an error output when using shell_exec(). It returned "fatal: could not read Password for"url": device not configured". i just had to add the password to the url in a http basic auth fashion. Now it all works fine. Thanks for the answers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM