git config not used when git pull via PHP file
I’m trying to
git pull from a PHP file, but it seems like the config isn’t being used.
git config --global user.name and
git config --global user.email are set and git pull works from the command line.
Now when I try to execute this script:
<?php header('Content-type: text/plain'); system('git pull 2>&1'); echo "\nCompleted!";
I get the following output:
* Please tell me who you are.
git config –global user.email “firstname.lastname@example.org” git config
–global user.name “Your Name”
to set your account’s default identity. Omit –global to set the
identity only in this repository.
fatal: empty ident not allowed
Is it maybe because the
git config is user specfic and PHP is ran from another user?
4 Solutions collect form web for “git config not used when git pull via PHP file”
Just use the
-c flag on git itself:
-c <name>=<value> Pass a configuration parameter to the command. The value given will override values from configuration files. The <name> is expected in the same format as listed by git config (subkeys separated by dots).
So something like:
git -c user.name=apache pull
should get you there. Add whatever arguments you need. If you want to specify a particular file, then either add the relevant configuration options to the user executing the script, or hack around it by exporting a different
HOME env variable first.
You expect your webserver to work even when you are not logged in, don’t you? This means it is run by another user than you (unless you specified otherwise). Unless you run your script via the CLI, the script is run by apache, www-data or whatever user you configured.
I managed to get it working, but it’s not an ideal solution. I need to find a way to set the
git config for the apache user once, instead of every time. I’m on shared host, so I don’t know if it’s actually possible.
<?php header('Content-type: text/plain'); system('git config user.name "Server"'); system('git config user.email "email@example.com"'); system('git pull 2>&1'); echo "\nCompleted!";
It’s a bit of kludge, but you can also set the system git config:
git config --system user.name <user> git config --system user.email <user>@<domain>
This has the rather unfortunate side effect of being the default config for all users of the server, so it really isn’t very good.